#160Medium
Subarray sums
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You are given an array of integers and a number . Count the non-empty subarrays whose sum is exactly .
Input format
The first line contains and (, ). The second line contains integers ().
Output format
A single number — the number of subarrays.
Examples
Input
5 7 2 -1 3 5 -2
Output
2
Note
The sum of is . Going from left to right, for every add how many earlier prefixes equal , then count itself. A dictionary (map/dict) works, or sorted prefixes with binary search.
There are negative numbers, so two pointers do not work. Prefixes reach , the answer .
Source: CSES, problem 1661.
