Two Sum II – Input Array Is Sorted
Given a 1-indexed array numbers sorted in non-decreasing order, find two numbers that add up to a specific target. Return their 1-based indices [index1, index2] with index1 < index2. Exactly one solution exists, each element may be used once, and you must use only constant extra space.
Open official problem prompt ↗Locate the unique pair of sorted values summing to target and report their 1-based positions using no extra data structure.
Two people stand at the low and high ends of a sorted number line and adjust: if their combined value is too small the low person steps up, if too big the high person steps down, converging on the exact total.
- Input
- numbers = [2, 7, 11, 15], target = 9
- Output
- [1, 2]
- Why
- numbers[1] + numbers[2] = 2 + 7 = 9, reported with 1-based indices.
2 <= numbers.length <= 3 * 10^4-1000 <= numbers[i] <= 1000numbers is sorted in non-decreasing order-1000 <= target <= 1000Exactly one valid answer exists