← All problems

Subarray Sum Equals K

Medium arrayshash mapsprefix-sums LeetCode ↗
Given an integer array and an integer k, count the number of contiguous subarrays whose elements sum to exactly k. The array may contain negative numbers, so a sliding window alone is insufficient.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), Space O(n)

Common pitfalls

  • Sliding window (fails with negatives)
  • Forgetting the {0:1} seed

Your solution

Log in to save your code and track progress.