What makes it slow, and the cue
After this: Find the question the inner loop repeats, and plan one pass with a hash map from value to position.
On this page · 5 sections · 3 checks
You have a correct answer that is too slow for a long list. Step 4 asks: what exactly makes it slow? Making it faster starts with that question, not with guessing a cleverer method.
The question the inner loop keeps asking
Look at what the inner loop does for one number. Take x = 2 at position 1, with target 6. The loop adds 2 to each later number and compares the sum with 6. Said another way, it is searching the list for one value: the number that 2 needs, which is 6 − 2 = 4.
So for every number x, the inner loop asks the same question: "Is the number I need, target − x, somewhere in the list, and where?" It answers by walking through the list. That walk is the slow part, and it happens once for every number.
A question of the form "is this value there, and where?" is a lookup by value. A hash map answers it in about one step, whatever the size of the map: you give it a key and it goes straight to where that key would be stored. The hash-maps tutorial shows why in The problem: finding things fast.
The cue was in the problem all along: "two items that add up to the target". For each number, the partner it needs is one exact value, and an exact value can be looked up.
The better plan
Go through the list once. Keep a map called seen from each number seen so far to its position. For each number x at position i:
- Work out
need, which is target − x. - If
needis inseen, the answer is its stored position and i. Stop. - Otherwise, store x with its position i, and go on.
Here is the plan on [3, 2, 4] with target 6, one row per number:
| i | x | need = 6 − x | need in seen? | seen after |
|---|---|---|---|---|
| 0 | 3 | 3 | no (seen is empty) | {3: 0} |
| 1 | 2 | 4 | no | {3: 0, 2: 1} |
| 2 | 4 | 2 | yes, at 1 | return [1, 2] |
Three numbers, three lookups. No pair was added up twice, and no walk through the list was needed.
The task below runs the same plan in the hash-map visualizer. The keys are the numbers and the values are their positions. Each step is a lookup or a store, in the order of the table.
- Look up 3Position 0 holds 3, so the number we need is 6 − 3 = 3. Look up 3. The map is empty, so it is not there.
- Insert 3 → 0
- Look up 4
- Insert 2 → 1
- Look up 2
- 0—
- 1—
- 2—
- 3—
- 4—
- 5—
- 6—
- 7—
Start with the first task above, or try any key.
- count
- 0
- load
- 0.00
- compares
- 0
Order matters: check first, then store
Step 2 comes before step 3 for a reason. Suppose you store first and check second. At position 0, you store 3 → 0. Then you work out need = 6 − 3 = 3, look it up, and find the 3 you stored a moment ago. The plan returns [0, 0]: the same item used twice, which is the trap from lesson 1.
Checking first means the map holds only numbers at earlier positions. A number can never find itself.
Check yourself
Which part of the two sum problem pointed to a lookup in a hash map?
The better plan runs on [3, 2, 4] with target 6. What does the map seen hold after the number at position 1 has been handled, and before position 2?
Someone swaps the order: each number is stored in seen first, and then the plan checks whether the needed number is there. What does that version return on [3, 2, 4] with target 6?
Honesty notes
- Find what makes the brute force slow: for each number x, it searches the list for target − x.
- Recognise the cue "add up to": the partner of each number is one exact value, which a hash map can look up in about one step.
- Walk the one-pass plan on [3, 2, 4] with a map from value to position: {} then {3: 0} then {3: 0, 2: 1}, then 2 is found at position 1.
- Check first, then store: storing first lets a number find itself and returns [0, 0].