Two Sum
Walk the array once. Standing on nums[i], its partner must be target − nums[i]. Keep a map value → index of the elements already passed. If the partner is in the map, we are done. If not, store nums[i] and move on.
nums[i] has a partner exactly when target − nums[i] is already in seen. Because seen only holds elements before i, the partner is always a different position.
Problem
You are given an integer array nums and an integer target.
Find two different positions i and j such that nums[i] + nums[j] == target, and return those two indices. Exactly one such pair exists. You may not use the same element twice, but two different elements may hold the same value. The indices can be returned in any order.
An integer array nums and an integer target.
A list of two indices [i, j] with i ≠ j and nums[i] + nums[j] == target.
nums[0] + nums[1] = 2 + 7 = 9.
2 + 4 = 6. Note that 3 + 3 is not allowed: there is only one 3, and it can't be used twice.
Two different elements with the same value are fine.
- • 2 ≤ nums.length ≤ 10^4
- • −10^9 ≤ nums[i] ≤ 10^9
- • −10^9 ≤ target ≤ 10^9
- • Exactly one valid answer exists.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I stand on
nums[i], which exact value would its partner need to have? - 2.
Do I need to search the whole array for that partner, or only the part I have already walked past?
- 3.
The answer asks for indices. What happens to the indices if I sort the array?
- 4.
How do I make sure an element is not used as its own partner, while
[3, 3]with target 6 still works? - 5.
Can
target − nums[i]overflow anint?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Check all pairs (i, j) with i < j | O(N²) | O(1) |
| Better | Sort copies that remember their index, then squeeze from both ends | O(N log N) | O(N) |
| Optimal | For each x, look up target − x in a map of earlier values; then store x | O(N) | O(N) |
Why does this belong to this pattern?
- “two numbers that add up to target”Once you pick one number, the other is fixed: `target − x`. That is a complement lookup.
- “return their indices”You must keep original positions. Store `value → index` in the map, and don't sort plain values.
- “array is not sorted”Two pointers need sorted order. Without it, use a hash map.
- “can't use the same element twice”Look up before inserting, so the map only holds earlier elements.
This is the anchor of the sub-pattern: for each element, compute the one partner it needs and ask the map if that partner was seen before. The next questions keep this shape but change what is stored. Contains Duplicate II stores the last index of each value and the "partner" is the same value nearby. Max Number of K-Sum Pairs stores counts, because each element can be used only once. If the array were already sorted (Two Sum II), two pointers would do it with O(1) space.
Don't make this mistake
This is a pattern-confusion trap: the two-pointer move is Two Sum II, where the array is sorted. "Sum too small → move left" is only safe when everything to the right is bigger. On [3, 2, 4], target 6, it checks 3+4=7, moves r, checks 3+2=5, moves l, and stops — it never finds 2+4.
Sorting moves elements, so l and r are positions in the sorted copy, not the original array. On [3, 2, 4], target 6, it returns [0, 2], but the right answer is [1, 2]. Sort (value, index) pairs, or use the map.
When nums[i] is exactly half of target, the lookup finds nums[i] itself. On [3, 2, 4], target 6, it returns [0, 0] — the same element used twice.
The two-pass version can work, but only with the extra != i check. Without it, [3, 2, 4] returns [0, 0]. The one-pass "look up, then insert" order gives you that check for free.
With target = −10^9 and nums[i] = 10^9, the result is −2·10^9, which is outside the int range. On LeetCode it is usually fine, but it is undefined behaviour in C++.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Arrays & Hashing translate into production architectures and low-level memory mechanics.