← All posts
November 25, 2026 · 3 min read

Subarray Sum Equals K in Python: Prefix Sums and a Hash Map

Subarray Sum Equals K (LeetCode 560): pin every running total on a tape, and a slice adds to k when two marks are k apart. One pass, O(n).

Subarray Sum Equals K asks how many contiguous slices of a list add up to exactly k. It is LeetCode 560. Our example is the list [1, 2, 1, 3, 1, 2] with k = 3.

The list 1, 2, 1, 3, 1, 2 as paper tiles under the question how many slices add up to 3?

The direct way tries every start and every end and adds up what lies between: O(n²) slices, and with a naive inner sum even O(n³). The trick below finds every matching slice in a single pass over the list.

What does a measuring tape have to do with it?

Lay a measuring tape under the list and walk along it, adding each number as you pass it. Put a mark on the tape at every running total, starting with a mark at 0 before the first number. For [1, 2, 1, 3, 1, 2] the marks land at 0, 1, 3, 4, 7, 8 and 10.

A slice is the stretch of tape between two marks. Its sum is the later mark minus the earlier one. So a slice adds to 3 exactly when two marks are 3 apart. The slice [1, 2] at indexes 0 and 1 runs from mark 0 to mark 3.

A measuring tape under the list with a pin at every running total, and tiles 1 and 2 marked in berry above a bracket from mark 0 to mark 3

How does a dictionary find the pairs?

Instead of comparing every pair of marks, walk the list once. For each number, add it to the running total, then look back k: if a mark already sits at total - k, every such mark starts a slice that ends here. A dictionary seen counts how many times each mark has been pinned, so the look-back is one lookup. Then pin the new mark.

The dictionary starts as {0: 1}: the mark at 0, before any number, so that slices starting at index 0 are counted too.

Pins at 0, 1, 3, 4, 7, 8 and 10, the readout totals: 1, 3, 4, 7, 8, 10, and the line that pins each total highlighted in the code

Which slices does it count?

Number Running total Look back to total - 3 Found? Slice Count
1 1 -2 no 0
2 3 0 yes [1, 2] (indexes 0 to 1) 1
1 4 1 yes [2, 1] (indexes 1 to 2) 2
3 7 4 yes [3] (index 3) 3
1 8 5 no 3
2 10 7 yes [1, 2] (indexes 4 to 5) 4

Three looks back to zero, four to one, seven to four and ten to seven: four slices.

Four brackets 3 wide, from 0 to 3, 1 to 4, 4 to 7 and 7 to 10, the last slice 1, 2 in berry, and the readout count = 4

What does the program look like?

This is the program from the video, line for line:

def subarraySum(nums, k):
    seen = {0: 1}
    total = count = 0
    for x in nums:
        total += x
        count += seen.get(total - k, 0)
        seen[total] = seen.get(total, 0) + 1
    return count

nums = [1, 2, 1, 3, 1, 2]
print(subarraySum(nums, 3))

Output:

4

We also checked subarraySum against a brute force that sums every slice, on 3,000 random lists with numbers from -5 to 5 and random k, and the answers always agree. LeetCode's own examples, subarraySum([1, 1, 1], 2) and subarraySum([1, 2, 3], 3), both return 2.

Why a dictionary and not two pointers?

With only positive numbers, a sliding window would also work, because the running total only grows. LeetCode 560 allows negative numbers and zeros, and then marks can repeat or move backwards along the tape. That is why seen stores a count per mark rather than a yes or no: subarraySum([1, -1, 1], 1) returns 3, because the mark at 1 is pinned twice. The dictionary handles all of it the same way.

How fast is it?

One pass over the list with one dictionary lookup and one update per number: O(n) time, and O(n) extra memory for the marks.

The readout count = 4, one pass: O(n) above the finished program

Try it

Keep the list [1, 2, 1, 3, 1, 2], but make k = 4. How many slices add up to 4? Find the marks that are 4 apart on the tape, then check your answer with the program.