Given an integer array nums and an integer k, return the total number of contiguous subarrays whose elements sum to exactly k.
Input / output
nums: int[], k: intExamples
nums = [1, 1, 1], k = 2 returns 2.nums = [1, 2, 3], k = 3 returns 2.nums = [1, -1, 0], k = 0 returns 3.Constraints
1 <= nums.length <= 20000-1000 <= nums[i] <= 1000-10^7 <= k <= 10^7Edge cases
0 are counted via an initial prefix sum of 0.Target complexity
O(n) time and O(n) extra space.Hints
prefix[j] - prefix[i]; you want that difference to equal k.current - k.Follow-up Why does the negative-number case rule out the classic shrinking sliding-window approach?