We need to count every contiguous subarray that sums to exactly k. The task is the total count, not a single subarray or the longest one.
A subarray is defined by its start and end indices. For an array of length n, there are n * (n + 1) / 2 possible subarrays. The brute force approach checks every single one, but can we do better?
There is a faster way using cumulative sums. If we know the cumulative sum from index 0 to index j (call it prefixSum[j]), and the cumulative sum from index 0 to some earlier index i (call it prefixSum[i]), then the sum of the subarray from i+1 to j is prefixSum[j] - prefixSum[i]. When that difference equals k, the subarray is valid. So for each position j, we count how many earlier prefix sums equal prefixSum[j] - k.
nums.length up to 2 * 10^4 puts O(n^2) at about 4 * 10^8 operations in the worst case, which is too slow, so an O(n) solution is preferred-1000 <= nums[i] <= 1000), which rules out sliding window approaches that rely on the sum growing as the window expandsk can be negative too, so we cannot assume anything about the target sum's signTry every possible subarray, compute its sum, and check if it equals k. We fix a starting index i, then extend the subarray one element at a time to each ending index j. Keeping a running sum as we extend avoids recomputing the sum from scratch each time.
count to 0i from 0 to n-1:sum to 0j from i to n-1:nums[j] to sumsum equals k, increment countcountn times, and for each iteration, the inner loop runs up to n times. Each inner iteration does O(1) work.The repeated forward scans are the cost. The next approach computes the prefix sum in a single pass and uses a hash map to look up, in O(1), how many earlier prefix sums form a valid subarray ending at the current position.
The sum of a subarray from index i+1 to index j equals prefixSum[j] - prefixSum[i]. For this to equal k, we need prefixSum[i] = prefixSum[j] - k. So as we walk through the array building up the prefix sum, at each position j we count how many earlier prefix sums equal currentPrefixSum - k.
We track those frequencies in a hash map and initialize it with {0: 1}. That entry accounts for subarrays starting at index 0, where the prefix sum itself equals k (and prefixSum - k is therefore 0).
The prefix sum turns "find subarrays with sum k" into "find pairs of prefix sums that differ by k," which mirrors Two Sum with a difference instead of an addition. Because we record each prefix sum only after using it for the lookup, every match pairs the current index with a strictly earlier one, so each counted subarray is non-empty and counted exactly once.
The {0: 1} seed covers subarrays starting at index 0. With nums = [3], k = 3, the prefix sum after the first element is 3, and we need prefixSum - k = 0 in the map to count it. Seeding 0 once provides that match.
count = 0, prefixSum = 0, and a hash map prefixCount with {0: 1}num in the array:num to prefixSumprefixSum - k exists in prefixCount, add its frequency to countprefixCount[prefixSum] by 1countn + 1 entries (including the initial {0: 1}).