Subarrays with Sum K
Count the contiguous subarrays whose sum is exactly . Values can be negative, so a sliding window doesn't work — combine prefix sums with a hash map.
Input
, then integers, then .
Output
The number of subarrays.
Constraints
,
Sample 1
Input
3 1 1 1 2
Output
2
Sample 2
Input
3 1 2 3 3
Output
2