Last Updated: March 29, 2026
We need to find two distinct positions in the array whose values sum to the target. The problem guarantees exactly one solution exists, so we don't need to worry about multiple matches or no matches.
The critical detail that shapes our approach: we need to return indices, not the values themselves. This means any approach that loses track of original positions (like sorting the raw array without remembering where each element came from) will need extra bookkeeping. The simplest way to think about it is this: for each element nums[i], we need to find whether target - nums[i] exists somewhere else in the array, and if so, where.
2 <= nums.length <= 10^4 → With n up to 10,000, an O(n^2) brute force runs 100 million operations in the worst case. That's borderline but should pass within typical time limits. O(n) is clearly preferred.-10^9 <= nums[i] <= 10^9 → Values can be negative. The complement target - nums[i] can also be negative. Don't assume positive numbers only.Only one valid answer exists → We can return as soon as we find the pair. No need to handle duplicates in the answer or check for multiple solutions.The most natural first thought: just check every possible pair. For each element, look at every other element to see if they add up to the target. This is what you'd do by hand if someone gave you a short list of numbers and asked you to find a pair.
It's not clever, but it works. And for small inputs, it's perfectly fine.
i, the inner loop picks the second element at index j (where j > i).(i, j), check if nums[i] + nums[j] == target.[i, j].This works but is slow for large inputs. For each element, we scan the rest of the array looking for its complement. What if we could check for the complement in O(1) instead of O(n)?
Here's the key insight: for each element nums[i], we know exactly what value we need to complete the pair. It's target - nums[i]. The brute force approach searches for this complement by scanning the array. But what if we could check for it instantly?
A hash map gives us O(1) lookups. So the idea is: as we iterate through the array, we store each element's value and index in a hash map. Before storing the current element, we check whether its complement already exists in the map. If it does, we've found our pair. If not, we add the current element and move on.
The beautiful thing is that we only need a single pass. By the time we reach the second element of the answer pair, the first element is already in the map.
The hash map acts as a "memory" of elements we've already visited. Each element is processed exactly once: we either find its complement already in the map (meaning the pair is complete) or we add it to the map for future elements to find. The order of operations (check first, insert second) ensures we never match an element with itself.
This is a textbook example of the "complement search" pattern: instead of searching for two things simultaneously, fix one and search for the other.
i.complement = target - nums[i].complement exists in the map. If yes, return [map[complement], i].nums[i] -> i to the map.