Module 4 · Hashing · Hashing

Subarray sum equals k

A subarray's sum is the difference of two prefix sums. Keep a running sum and a HashMap of how many prefixes reached each sum: every earlier prefix equal to currentSum - k starts a subarray that ends here and sums to k.

A highlighted window on nums is a subarray that sums to k. prefixValueToCount maps each prefix sum to how many prefixes reached it, and subarrays lists those found as [start..end].