Day 15 of 90MediumLeetCode

Subarray Sum Equals K

Count how many contiguous subarrays have a sum exactly equal to k.

Pattern
Prefix sum + hash map
Time box
25 min
Time
O(n)
Space
O(n)

Try it first — give it 25 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    Given an array nums (which may contain negative numbers) and an integer k, count the contiguous subarrays whose elements add up to exactly k.

    • nums = [1, 1, 1], k = 2 → 2 (the subarrays [1,1] at positions 0–1 and 1–2)
    • nums = [1, 2, 3], k = 3 → 2 ([1,2] and [3])

    Start with brute force

    Try every start j and extend the end i, keeping a running sum. Count the times it equals k. That's O(n²) — every subarray is checked once.

    Why not a sliding window? Windows work when all numbers are positive (growing the window only increases the sum). Here numbers can be negative, so shrinking or growing doesn't move the sum predictably. That rules the window out and points us to prefix sums.

    Building the intuition

    Define prefix as "the sum of everything so far". The sum of a subarray is the difference of two prefixes:

    nums:      3   4   7   2  -3   1
    prefix:    3   7  14  16  13  14
    
    sum of nums[2..3] = 7 + 2 = 9 = prefix[3] − prefix[1] = 16 − 7

    So when we're standing at index i with running prefix P, a subarray ending here sums to k exactly when an earlier prefix equals P − k.

    That is Two Sum again: for the current value, we know exactly which partner we need. And since we're counting, the map stores how many times each prefix sum has appeared.

    The {0: 1} seed. A subarray that starts at index 0 has nothing before it, which is like an "empty prefix" with sum 0. Seeding the map with 0 → 1 counts those subarrays.

    The optimal approach

    1. count = {0: 1}, prefix = 0, answer = 0.
    2. For each number x:
      • prefix += x
      • answer += count[prefix − k] (0 if missing)
      • count[prefix] += 1
    3. Return answer.

    Look up before recording the current prefix, or you'd pair a prefix with itself (an empty subarray) whenever k = 0.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    from collections import defaultdict
    
    
    class Solution:
        def subarraySum(self, nums: List[int], k: int) -> int:
            count = defaultdict(int)  # prefix sum -> how many times we've seen it
            count[0] = 1              # the empty prefix, for subarrays starting at 0
            prefix = 0
            answer = 0
    
            for x in nums:
                prefix += x
                answer += count[prefix - k]   # earlier prefixes that complete a sum of k
                count[prefix] += 1            # record AFTER looking up
    
            return answer

    Dry run

    nums = [1, 2, 3], k = 3

    xprefixneed = prefix − 3count beforeaddanswer
    11−2{0:1}00
    230{0:1, 1:1}1 ([1,2])1
    363{0:1, 1:1, 3:1}1 ([3])2

    Answer: 2.

    Complexity

    • Time: O(n). One pass, O(1) average per map operation.
    • Space: O(n). Up to n distinct prefix sums in the map.

    Edge cases and traps

    • Forgetting {0: 1}. You'll miss every subarray that starts at index 0.
    • Negative numbers. They're exactly why a sliding window fails; prefix sums don't care.
    • k = 0. Works as long as you look up before you insert.
    • Using a set instead of a map. The same prefix can appear many times — each occurrence is a different subarray. You need counts.

    Say it in the interview

    "Negative numbers rule out a sliding window. A subarray sum is prefix[i] minus prefix[j], so for each position I need the number of earlier prefixes equal to prefix minus k. I keep a hash map of prefix sum to count, seeded with zero once for subarrays starting at index 0. One pass, O(n) time and space."

    Follow-ups interviewers ask

    • "Return the longest such subarray instead?" Store the first index of each prefix sum instead of a count.
    • "Count subarrays whose sum is divisible by k?" (LeetCode 974) Use prefix mod k as the key — watch out for negative remainders.
    • "What if all numbers are positive?" A sliding window works and uses O(1) space.

    Practise the same idea

    This is Day 15 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free