#C5201. Subarray Sum
Subarray Sum
Subarray Sum
This problem requires you to find the number of contiguous subarrays within a given array whose sum equals a specified target k. A subarray is defined as a contiguous sequence of elements within an array. To solve this efficiently, you may consider using cumulative sums and hashing techniques. In particular, if you define \(S_i\) as the cumulative sum up to the \(i\)-th element, then the sum of the subarray from index \(i+1\) to \(j\) can be computed as \(S_j - S_i\). Your task is to read input from stdin
and output the answer for each test case to stdout
.
inputFormat
The input is given via stdin
and is formatted as follows:
- The first line contains an integer \(T\), the number of test cases.
- For each test case, the first line contains two space-separated integers \(n\) and \(k\) where \(n\) is the number of elements in the array and \(k\) is the target sum.
- The second line of each test case contains \(n\) space-separated integers representing the array elements.
outputFormat
For each test case, print a single integer on a new line representing the number of subarrays whose sum is equal to \(k\). The output should be sent to stdout
.
3
3 2
1 1 1
4 0
1 2 -2 3
4 0
1 -1 1 -1
2
1
4
</p>