Loading...
Loading...
Loading...
Count the contiguous subarrays of a list of integers whose sum equals k. Numbers may be negative.
Example 1
Input
1 2 3 -3 3 3
Output
5
[1,2], [3], [1,2,3,-3], [3,-3,3], [3].
Example 2
Input
1 1 1 2
Output
2
Try the problem first. Open one hint at a time.
A subarray sum is a difference of two prefix sums.
Keep a dictionary counting how often each prefix sum occurred.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor