Two positions that add up
You are given a list of whole numbers nums and a number target. Exactly one pair of different positions i < j has nums[i] + nums[j] == target.
Return the two positions as [i, j], the smaller one first.
Checking every pair works for short lists but is far too slow for long ones. Can you do it in one pass?
- Input
nums = [4,9,1,7]
target = 8- Output
[2,3]
1 + 7 = 8, and no other pair makes 8.
- Input
nums = [3,6,3]
target = 6- Output
[0,2]
The two 3s, at positions 0 and 2.
Limits
2 <= nums.length <= 100000-10^8 <= nums[i] <= 10^8-2 * 10^8 <= target <= 2 * 10^8Exactly one pair adds up to target.
Hints
Hint 1
For each number, the partner it needs is target minus that number.
Hint 2
Remember where you have seen each number so far.
Where this was reported
- Infosys, Specialist Programmer interview, 2026 (GeeksforGeeks)
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
