← All Problems⏱ 0:00
p-1000
Two Sum
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 has exactly one solution, and you may not use the same element twice.
Constraints
- 2 ≤ nums.length ≤ 10^5
- -10^9 ≤ nums[i] ≤ 10^9
- -10^9 ≤ target ≤ 10^9
- Exactly one valid answer exists
Examples
Input
nums = [2,7,11,15], target = 9
Output
[0,1]
Explanation
We need to find two numbers in the array whose sum equals the target value 9. The number at index 0 is 2. The number at index 1 is 7. 2 + 7 = 9, which matches the target. So, we return their indices: [0, 1].
Input
nums = [3,2,4], target = 6
Output
[1,2]
Explanation
We need to find two numbers in the array whose sum equals the target value 6. The number at index 1 is 2. The number at index 2 is 4. 2 + 4 = 6, which matches the target. So, we return their indices: [1, 2].
Hints
💡 Hint 1
Code
Test Result
Run your code to see results here...
