Four solutions run step by step next to their Java code, from the slowest to the fastest. Read the problem first, then pick an input or type your own and every solution reruns on it.
You get a list of whole numbers and one target number. Exactly two of the numbers add up to the target. Your job is to say where they are.
What you get
An array nums with at least two integers, and an integer target. The numbers can be negative, zero or repeated, and they are in no particular order.
What you return
The two positions [i, j] where nums[i] + nums[j] == target. Positions (indexes), not the values. Either order of the two is accepted.
The rules
There is always exactly one answer, so you never have to report "not found".
You may not use the same position twice. Two equal values at two different positions are fine.
The answer refers to the original positions, so if you reorder the numbers you must remember where each one came from.
Try it by hand
Input
nums = [3, 8, 5, 1, 10], target = 9
Output
[1, 3]
30815213104
value on top, index below
Take position 0, which holds 3. Its partner would have to be 9 − 3 = 6. There is no 6 in the array, so 3 is not part of the answer.
Take position 1, which holds 8. Its partner would have to be 9 − 8 = 1.
Look for a 1. It is at position 3, which is a different position from 1, so it is allowed.
The answer is [1, 3], because 8 + 1 = 9.
Edge cases to check
[3, 3] with target 6 gives [0, 1]: the same value at two positions.
[3, 2, 4] with target 6 gives [1, 2]. Using the 3 twice would also make 6, but that is one position used twice.
[-4, 7, 2] with target -2 gives [0, 2]. Negative numbers follow the same rule.
With only two numbers, the answer is always [0, 1].
The question behind the solutions
Checking every pair always works, but n numbers make about n² ÷ 2 pairs: 50 million checks for 10,000 numbers. For each number you already know the partner you need. How do you find out whether that partner exists without scanning the whole array again?
Four ways to find the pair
The example from above, nums = [3, 8, 5, 1, 10] and target 9, sketched the way you would draw it in an interview. Each board shows what the approach keeps in memory and the steps it runs. Then step through all four below.
1. Brute force TwoSumBruteForce
Remembers: nothing but the two loop positions.
For each position i, try every later position j.
If nums[i] + nums[j] is the target, return [i, j].
Starting j after i means no pair is tried twice and no position is used twice.
The catch: n numbers make about n² ÷ 2 pairs. Here it took 6 tries; 10,000 numbers can take 50 million.
2. Sort + two pointers TwoSumSortTwoPointers
Remembers: a sorted copy of (value, original index) pairs.
Pair each value with its index and sort the pairs by value.
Put L on the smallest value and R on the largest.
Sum too big: move R left. Too small: move L right. Equal: return the two original indexes.
The catch: sorting costs O(n log n), and it loses the positions unless you carry them along, as the "was at" row does.
3. Hash map, two passes TwoSumHashMapTwoPass
Remembers: a map from every value to its index.
First pass: store value → index for every number.
Second pass: for each i, work out need = target − nums[i].
If need is in the map at a different index, return both.
The catch: it always reads the whole array twice and stores all n numbers, even when the answer is near the start.
4. Hash map, one pass TwoSumHashMapOnePass
Remembers: a map of the values seen so far.
Work out need = target − nums[i].
If need is already in the map, return [map[need], i].
Otherwise store nums[i] → i and move on.
The catch: it still needs O(n) extra memory. Checking before storing is what stops a number from pairing with itself.