Return how many contiguous runs of nums (subarrays) add up to exactly k. Values can be negative or zero. Runs at different positions count separately even if they hold the same values.
The starter code tries every start and end. It is correct but too slow for long lists. Make it fast.
Example 1
Input:
count_subarrays_sum_k([1, 1, 1], 2)
Output:
2
The runs are positions 0 to 1 and positions 1 to 2.
Example 2
Input:
count_subarrays_sum_k([1, -1, 0], 0)
Output:
3
The runs [1, -1], [0] and [1, -1, 0] all add to 0.
0 <= len(nums) <= 2 * 10**5-10**4 <= nums[i] <= 10**4-10**9 <= k <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.