← All problems
Subarray Sum Equals K
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.
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.