Two Sum
Find the two numbers in a sorted array that add up to a target.
You are given an array numbers sorted in ascending order, and an integer target. Return the indices of the two values that add up to the target, smallest index first.
Constraints
- 2 ≤ numbers.length ≤ 10,000
- -10⁹ ≤ numbers[i] ≤ 10⁹
- numbers is sorted in ascending order
- assume exactly one pair works
Examples
- Input
- numbers = [2,7,11,15], target = 9
- Output
- [0,1]
- Why
- numbers[0] + numbers[1] = 2 + 7 = 9.
- Input
- numbers = [2,3,4], target = 6
- Output
- [0,2]
- Why
- 2 + 4 = 6. Note that 3 + 3 is not allowed - that would reuse one element.
- Input
- numbers = [-3,3,4,90], target = 0
- Output
- [0,1]
- Why
- Negative numbers behave no differently.
Submitting also runs 6 hidden tests.