#C4040. Subarray Sum Equals K
Subarray Sum Equals K
Subarray Sum Equals K
Given an array A
of integers of size N
and an integer K
, find the number of contiguous subarrays (subsegments) whose sum is exactly equal to K
. The array can contain negative numbers, positive numbers, or zeros. An efficient solution with a time complexity of \(O(N)\) is expected due to potentially large input sizes.
Details:
- The input consists of two lines. The first line contains two integers
N
andK
. - The second line contains
N
space-separated integers representing the arrayA
. - Output a single integer representing the number of subarrays whose sum is exactly equal to
K
.
Example:
Input: 5 5 1 2 3 4 5</p>Output: 2
inputFormat
The first line of input 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 contains \(N\) space-separated integers representing the elements of the array \(A\).
outputFormat
Output a single integer which is the number of contiguous subarrays whose sum is exactly \(K\).
## sample5 5
1 2 3 4 5
2