Problem and examples
You get an array of integers numbers that is already sorted from small to big, and an integer target.
Find the two numbers that add up to target and return their positions counting from 1 (1-indexed): `[index1, index2]` with index1 < index2.
Every input has exactly one answer, and you may not use the same element twice. Use only constant extra space.
Example 1Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
2 + 7 = 9: positions 1 and 2 (counting from 1).
Example 2Input: numbers = [2,3,4], target = 6
Output: [1,3]
2 + 4 = 6: positions 1 and 3. The 3 cannot pair with itself.
Example 3Input: numbers = [-1,0], target = -1
Output: [1,2]
-1 + 0 = -1: positions 1 and 2.
Example 4Input: numbers = [2,3,6,9], target = 9
Output: [2,3]
The video's dry run: 2 + 9 = 11 too big, drop 9; 2 + 6 = 8 too small, drop 2; 3 + 6 = 9. Positions 2 and 3.
- 2 ≤ numbers.length ≤ 3 · 10⁴
- -1000 ≤ numbers[i] ≤ 1000
- numbers is sorted in non-decreasing order
- -1000 ≤ target ≤ 1000
- Exactly one valid answer exists.