Two Sum II
Medium · two pointers
The list numbers is sorted from smallest to largest (equal values may sit next to each other). Exactly one pair of positions i < j has numbers[i] + numbers[j] == target.
Return [i + 1, j + 1]: the two positions counted from 1, smaller position first.
Each position can be used only once, so a value that appears a single time cannot be paired with itself. The input is guaranteed to contain a valid pair.
Examples
Example 1
Input: numbers = [2, 7, 11, 15], target = 18
Output: [2, 3]
7 + 11 = 18, and 7 and 11 sit at positions 2 and 3 when counting from 1.
Example 2
Input: numbers = [-4, 0, 3, 3, 9], target = 6
Output: [3, 4]
The two 3s are different positions, so they may be paired with each other.
Example 3
Input: numbers = [1, 5], target = 6
Output: [1, 2]
Constraints
- 2 <= len(numbers) <= 3 * 10^4
- -10^9 <= numbers[i] <= 10^9
- numbers is sorted in non-decreasing order.
- Exactly one pair of positions adds up to target.