Two sum
EasyStuck?
Show a hintHide the hint
For each number, ask whether you have already seen the one it needs. A hash map answers that in constant time.
The ideas behind this problem
Last problem in this lesson
Go to the next lessonGiven an array of integers and a target value, return the indices of the two numbers that add up to the target.
Each input has exactly one solution, and you may not use the same element twice. Return the indices in increasing order.
Constraints#
2 <= n <= 100000-10^9 <= nums[i], target <= 10^9
Input
nums is a count then that many integers. target is one integer.
Output
Two space-separated indices, zero-based.
Examples
Input
4 2 7 11 15 9
Output
0 1
2 + 7 = 9, so the answer is the indices of those two values.
Input
3 3 2 4 6
Output
1 2
Something wrong with this problem? Report an issue
Ready to try it
Write your solution in C, C++, Java or Python and see every test case come back with the time and memory it took.
Free, and it stays free. No card.