Practice Two Sum
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Practice approach: Scan left to right while mapping each previously seen value to an index. Before storing the current value, look for target minus that value. This order prevents using the current index twice and still handles equal values: for [3, 3] and target 6, the first 3 is stored and the second finds it. Expected time is O(n) with a hash map and O(n) additional space. Return indices rather than values, and test both a repeated-value pair and a pair separated by unrelated entries.
Source: Two Sum on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [2,7,11,15], target = 9
[0,1]
# Example 2
nums = [3,2,4], target = 6
[1,2]
# Example 3
nums = [3,3], target = 6
[0,1]
Constraints
How to solve Two Sum
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
The partner of each element is fully determined as target minus that element, so the question becomes whether that partner has already been seen, which a hash map keyed by value answers in constant time.
Why brute force is not enough
Test every pair of indices and return the first whose values sum to target. That is O(n^2) comparisons, roughly 5 x 10^7 for n up to 10^4; it passes here but is quadratic, and an interviewer expects the linear hash-map solution.
Approach
- Create an empty hash map from value to the index at which that value was seen.
- Scan the array left to right. For the current element compute its complement, target minus the value.
- Look the complement up before inserting the current element. If it is present, the stored index and the current index are the answer; because the map only ever holds earlier indices, the two indices are guaranteed to differ.
- If it is absent, insert the current value with its index and continue. Since exactly one solution exists, the scan always terminates with a match.
Walkthrough of Example 2
nums = [3,2,4], target = 6 → [1,2]
- Index 0 holds 3; the complement is 3, the map is empty, so store 3 at index 0.
- Index 1 holds 2; the complement is 4, not present, so store 2 at index 1.
- Index 2 holds 4; the complement is 2, which is found at index 1.
- Return [1,2]. Checking before inserting is what prevented the 3 at index 0 from pairing with itself.
Time and space
Time O(n) · Space O(n). Each of the n elements is processed once with constant-time hash operations, and the map may hold up to n entries; n is the length of nums.
Interview pitfalls
- Inserting the current element before checking for its complement, which lets a value pair with itself when target is twice that value (for [3,2,4] and target 6 this wrongly returns [0,0]).
- Sorting the array to use two pointers and then returning positions in the sorted order rather than the original indices.
- Assuming values are small enough for a direct-address array; values range from -10^9 to 10^9 and may be negative, so a hash map is required.
- Worrying about duplicates overwriting map entries: it is harmless, because the complement check happens before the overwrite and only one valid answer exists.
Follow-up questions interviewers ask
- If the input array were sorted, could you solve it in O(1) extra space, and how would two pointers work?
- How would you return every pair, or count the pairs, when several solutions and repeated values exist?
- How does the idea extend to three numbers summing to a target, and what time complexity should you expect?
Further reading on the technique: Hash table
Related LeetCode Problems
Practice more problems similar to Two Sum.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.