Two Sum in Python with a hash map, in one pass
Two Sum (LeetCode 1) in one pass: each number asks a dictionary whether its partner was already seen. O(n) instead of checking every pair.
Two Sum is LeetCode problem 1: given a list of numbers and a target, find the two positions whose numbers add up to the target. The classic fast answer uses a hash map, and the idea behind it is simple: walk the list once and remember every number you've seen.
What is the slow way?
Take the numbers 4, 9, 2, 7 and 5, and a target of 12. The obvious approach is to try every pair: 4 with 9, 4 with 2, and so on until one pair adds up. Five numbers make 10 pairs, and here the only pair that works, 7 and 5, is the very last one you'd try.

The number of pairs grows like n squared. A thousand numbers is about half a million pairs; a million numbers is about half a trillion.
What does remembering change?
Go through the list one number at a time. For each number, its partner is the target minus itself: for a target of 12, a 4 needs an 8 and a 9 needs a 3. So each number asks one question: have I already seen my partner?
If the answer is no, the number is stored in a box of seen numbers, together with its index, its position in the list. Later numbers can then find it there.
In Python, that box is a dictionary called seen that maps each number to its index. Asking "has the 8 been seen?" is need in seen, and a dictionary answers that in one step on average, no matter how many numbers it holds.
What happens with 4, 9, 2, 7, 5?
The 4 comes first. It needs an 8. Nothing has been seen yet, so the 4 is stored with index 0.

Then the others, one at a time:
| Index | Number | Needs | Seen already? | seen after |
|---|---|---|---|---|
| 0 | 4 | 8 | no | {4: 0} |
| 1 | 9 | 3 | no | {4: 0, 9: 1} |
| 2 | 2 | 10 | no | {4: 0, 9: 1, 2: 2} |
| 3 | 7 | 5 | no | {4: 0, 9: 1, 2: 2, 7: 3} |
| 4 | 5 | 7 | yes, index 3 | done |
When the 5 arrives it needs a 7, and the 7 was seen at index 3.

The answer is the partner's index and the current index: [3, 4]. Notice the order of the two steps in the loop: we ask before we store the number. That way a number never pairs with itself, so a 6 with a target of 12 needs a second 6.
What does the program look like?
This is the program from the video, line for line:
nums = [4, 9, 2, 7, 5]
target = 12
def two_sum(nums, target):
seen = {} # number -> index
for i, x in enumerate(nums):
need = target - x
if need in seen:
return [seen[need], i]
seen[x] = i # remember: index i
print(two_sum(nums, target)) # [3, 4]
Output:
[3, 4]
![The answer [3, 4]: index 3 for the 7 and index 4 for the 5, with the full Python program below](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/alg-004-two-sum/answer.jpg)
Why is this faster?
The loop visits each number once, and each visit does one dictionary lookup and at most one insert. That is 5 checks for 5 numbers, and n checks for n numbers: O(n) time. Checking every pair took 10 checks here and grows like n squared.

The price is memory: seen can hold up to n numbers, so the dictionary uses O(n) extra space. That is the trade at the heart of many fast algorithms: remember what you've seen, and you never have to look for it again.
| Approach | Time | Extra space |
|---|---|---|
| Every pair | O(n²) | O(1) |
| Hash map, one pass | O(n) | O(n) |
Try it
Keep the same numbers, 4, 9, 2, 7 and 5, but change the target to 11. Two different pairs now add up to 11. Which indices does two_sum return, and why that pair and not the other?