#K60622. Count Subarrays with Sum
Count Subarrays with Sum
Count Subarrays with Sum
Given an array of integers and an integer \( k \), your task is to compute the number of contiguous subarrays whose sum equals \( k \). This problem requires an efficient solution that typically runs in \( O(n) \) time by utilizing prefix sums and a hash map.
Example:
Input: 5 5 1 2 3 4 5 Output: 2
In the above example, the subarrays [2, 3] and [5] sum to 5.
inputFormat
The input is given from standard input (stdin) in the following format:
n k a1 a2 a3 ... an
Here, \( n \) is the number of elements in the array, \( k \) is the target sum, and \( a1, a2, \dots, an \) are the elements of the array.
outputFormat
Output a single integer which is the number of contiguous subarrays that sum to \( k \). The result should be printed to standard output (stdout).
## sample5 5
1 2 3 4 5
2