Two Pointers
LC #167Medium
Two Sum II - Input Array Is Sorted
Two Pointers
AmazonGoogleAppleProblem
Find two numbers in a sorted array that add up to a target. Return their 1-indexed positions.
arraytwo-pointersbinary-search
Constraints
- ›2 ≤ n ≤ 3 × 10⁴
- ›-1000 ≤ numbers[i] ≤ 1000
- ›Sorted in non-decreasing order
- ›Exactly one solution guaranteed
Example
Input
numbers = [2, 7, 11, 15], target = 9Output
[1, 2]Why
numbers[1] + numbers[2] = 2 + 7 = 9 (1-indexed)