Find two numbers that add up to target in a sorted array using constant extra space O(1).
Given a 1-indexed array of integers `numbers` that is already sorted in non-decreasing order, find two numbers such that they add up to a specific `target` number. Return the indices of the two numbers, `[index1, index2]`, as an integer array of length 2 where 1 <= index1 < index2 <= numbers.length. You may not use the same element twice. Your solution must use only constant extra space.
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. Return [1, 2].
Invariant: At each step, if a solution exists in the original array, it is guaranteed to lie within the inclusive subsegment [left ... right]. All pairs outside this interval have been proven invalid.
Sorting provides monotonic leverage. At each step, one pointer increment or decrement discards an entire slice of invalid combinations without memory overhead.
Bottleneck: Brute-force nested loops inspect N*(N-1)/2 pairs -> O(N²). Two Pointers uses the monotonicity invariant to discard N-1 impossible pairs in each step.
Find two numbers that add up to target in a sorted array using constant extra space O(1).
Given a 1-indexed array of integers `numbers` that is already sorted in non-decreasing order, find two numbers such that they add up to a specific `target` number. Return the indices of the two numbers, `[index1, index2]`, as an integer array of length 2 where 1 <= index1 < index2 <= numbers.length. You may not use the same element twice. Your solution must use only constant extra space.
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. Return [1, 2].
Invariant: At each step, if a solution exists in the original array, it is guaranteed to lie within the inclusive subsegment [left ... right]. All pairs outside this interval have been proven invalid.
Sorting provides monotonic leverage. At each step, one pointer increment or decrement discards an entire slice of invalid combinations without memory overhead.
Bottleneck: Brute-force nested loops inspect N*(N-1)/2 pairs -> O(N²). Two Pointers uses the monotonicity invariant to discard N-1 impossible pairs in each step.
Initialized pointers: Left at index 0 (2), Right at index 3 (15). Looking for target sum = 9.