#K11561. Count Subarrays with Target Sum

    ID: 23496 Type: Default 1000ms 256MiB

Count Subarrays with Target Sum

Count Subarrays with Target Sum

You are given an integer sequence and a target integer \(x\). Your task is to count the number of contiguous subarrays (segments) of the sequence whose sum is exactly equal to \(x\).

For example, if the sequence is [1, 2, 1, 2, 3] and \(x = 5\), there are 2 subarrays whose sum is 5: namely, [2, 1, 2] and [1, 2, 2] (depending on indices). Use an efficient algorithm ideally based on prefix sums, since the sequence expected can be large.

inputFormat

The first two numbers are integers \(n\) and \(x\) where \(n\) is the length of the sequence and \(x\) is the target sum. This is followed by \(n\) space-separated integers representing the sequence.

outputFormat

Output a single integer: the count of contiguous subarrays whose sum equals \(x\).

## sample
5 5 1 2 1 2 3
2