Mental Model
We track sums of numbers from the start to each position and use a map to find if a previous sum lets us form a subarray summing to k.
Analogy: Imagine walking along a path and counting steps. If you know the total steps at two points, the difference tells you how many steps you took between them. We use a map to remember these counts to quickly find matching distances.
Array: [1, 2, 3, 4]
Prefix sums: [0, 1, 3, 6, 10]
Map stores sums seen so far:
{0:1} -> {0:1, 1:1} -> {0:1, 1:1, 3:1} -> {0:1, 1:1, 3:1, 6:1} -> {0:1, 1:1, 3:1, 6:1, 10:1}
We check if (current_sum - k) exists in map to find subarray.