The problem. Given an integer array nums and an integer k, return how many contiguous subarrays add up to exactly k. The numbers can be negative.
Input: nums = [1, 1, 1], k = 2
Output: 2 [1, 1] starting at index 0, and [1, 1] starting at index 1
Input: nums = [1, 2, 3], k = 3
Output: 2 [1, 2] and [3]Because values can be negative, adding an element can make a sum smaller — so the usual sliding window (grow while too small, shrink while too big) doesn't work here. We need a different way to avoid re-adding numbers.
Try every start i and end j, add up nums[i..j] with a third loop, and count the ones that equal k.
function subarraySum(nums, k) { let count = 0; for (let i = 0; i < nums.length; i++) { for (let j = i; j < nums.length; j++) { let sum = 0; for (let t = i; t <= j; t++) sum += nums[t]; // nums[i..j], from scratch if (sum === k) count++; } } return count;}Time O(n³), space O(1). Each subarray's sum is rebuilt from nothing, even though it's just the previous one plus one more number.
Fix the start i and grow the end j, adding one number each step. Every subarray is still checked, but each check is now O(1).
function subarraySum(nums, k) { let count = 0; for (let i = 0; i < nums.length; i++) { let sum = 0; for (let j = i; j < nums.length; j++) { sum += nums[j]; // sum of nums[i..j] if (sum === k) count++; } } return count;}Time O(n²), space O(1). Better — but there are n²/2 subarrays, and we still look at every one. To go faster, we must count subarrays without visiting them one by one.
Let prefix be the running total of everything up to the current index. The sum of any subarray is a difference of two prefix sums: everything up to its end, minus everything before its start.
So a subarray ending here sums to k exactly when some earlier prefix equals prefix − k. Instead of searching for it, keep a hash map from each prefix sum to how many times it has occurred. Then one lookup counts every matching start at once. The map begins with {0: 1} — the empty prefix before index 0 — so subarrays that start at index 0 are counted too.
Watch the map grow, and how one lookup can count several subarrays at once. The negative-numbers example shows why a sliding window couldn't work.
Time O(n), space O(n). One pass, one lookup and one update per element. The order matters: look up before recording the current prefix, or an empty subarray could be counted when k = 0.
O(n³)About n²/2 subarrays, each summed again from scratch in O(n).
O(1)Only loop indexes and a running total.
[1, -1, 0] returns 3: [1, -1], [0] and [1, -1, 0].[0, 0, 0] with k = 0 returns 6; counts in the map, not just presence, make this work.[3, 4, 7, 2, -3, 1, 4, 2] with k = 7 returns 4.[1] with k = 0 returns 0."Prefix sum + hash map" is a reusable recipe: the longest subarray with sum k, subarrays divisible by k, and counting subarrays with equal 0s and 1s (turn 0s into −1s, then look for sum 0) all follow it.
O(n²)Still visits all n²/2 subarrays, but each sum grows by one number in O(1).
O(1)One running sum per start.
O(n)One pass, with one lookup and one update per element.
O(n)The map can store up to n different prefix sums.