The first time I solved Two Sum, I was proud of the brute force. It worked. Then I saw the follow-up: “Can you do it in one pass?” and I had no idea why one pass was even possible. Nearly everyone learns the hash map trick. But understanding WHY one pass is enough — that is the difference between memorizing a solution and owning a pattern.
Here is the problem: given an array of integers and a target sum, find two numbers that add up to the target and return their indices. It sounds trivial. A small array, a simple target — what could go wrong?
Let us start with an easy case to see why this problem feels so approachable at first.
Given 271115 and target = 9, find the pair. Go ahead — check the obvious candidates.
Which pair sums to 9? Tap to check.