Subarray Sum Equals K
intermediateCounts subarrays with sum exactly equal to k using prefix sums and a HashMap; O(n) time and space.
1
0
2
1
3
2
-2
3
2
4
Current index
Array element
prefixSum added to map
Target found (hit)
Initialise: prefixSum = 0, map = {0: 1}. The seed "0" accounts for subarrays starting at index 0.
1 / 16