← All problems

Subarray Sum Equals K (hashmap drill)

Medium hash mapsprefix-sumsarrays LeetCode ↗
Count the number of contiguous subarrays summing to k using prefix sums and a hash map, in O(n) time. Explain why the map stores prefix-sum frequencies.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), Space O(n)

Common pitfalls

  • Query-after-update order swapped
  • Sliding window on negatives

Your solution

Log in to save your code and track progress.