Given an integer array `nums` and an integer `k`, return the total number of subarrays whose elements sum to `k`. A subarray is a contiguous non-empty sequence of elements within the array. For example, given nums = [1,1,1] and k = 2, there are two subarrays that sum to 2: nums[0..1] = [1,1] and nums[1..2] = [1,1], so the answer is 2.
Example 1
Input: nums = [1,1,1], k = 2
Output: 2
Explanation: The two subarrays that sum to 2 are [1,1] at indices 0-1 and [1,1] at indices 1-2. They overlap but count separately.
Example 2
Input: nums = [1,2,3], k = 3
Output: 2
Explanation: [1,2] sums to 3 and so does [3] on its own, so there are 2 subarrays.
Example 3
Input: nums = [1], k = 0
Output: 0
Explanation: The only subarray is [1], whose sum is 1, not 0.
If prefix_sum[j] - prefix_sum[i] = k, then the subarray from i+1 to j sums to k. A hash map counting previous prefix sums lets us find all such i in O(1).
Run checks all cases above. Submit evaluates all test cases.