easy
Two Sum
You are given an array of integers nums and an integer target. Return the indices of the two distinct elements whose values add up to target. Exactly one such pair exists, and the array is not sorted.
Constraints
- 2 ≤ n ≤ 10^4
- -10^9 ≤ nums[i], target ≤ 10^9
- Exactly one valid answer exists
Examples
in: nums = [3, 8, 11, 15], target = 19
out: [1, 2]
8 + 11 = 19.
in: nums = [2, 4, 4], target = 8
out: [1, 2]
Code it yourself
Solve in
Practice journal →Draft saved in this browser.
Public test cases · contract v1
Arguments are passed to your function. Tests are public practice checks, not hidden interview grading. Passing does not prove every possible input.
- Complement in middle
[[3,8,11,15],19] → [1,2]
- Duplicate values, distinct indices
[[2,4,4],8] → [1,2]
- Negative values
[[-4,-2,9],-6] → [0,1]
- Zero complement
[[0,5,0],0] → [0,2]
- Minimal input
[[7,3],10] → [0,1]
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.