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 their indices as [index1, index2] (1-indexed). You may not use the same element twice. Use only constant extra space.
numbers = [2,7,11,15], target = 9[1,2]numbers = [2,3,4], target = 6[1,3]Array is sorted. Sum too small → only moving L right can increase it (left value grows). Sum too large → only moving R left can decrease it (right value shrinks). This monotonicity guarantees we find the unique solution in a single pass — no brute-force needed.
The brute force checks every pair, which is O(n²). A hash map gets it down to O(n) time, but it needs O(n) extra space, and that's not allowed here. Neither one uses the fact that the array is sorted.
Put one pointer at the start and one at the end. If their sum is too big, the right number is too large, so move the right pointer left. If the sum is too small, move the left pointer right. Every move throws away a number that can never be part of the answer.
In a sorted array, two pointers from both ends find a pair in one pass. Sum too big, move right in. Sum too small, move left in.
Run the animation above on your own input, or watch the 1:56 video explanation of Two Sum II with a dry run and Java code.