Maintained by Vikas Dulgunde, software engineer
You are handed an array called numbers that is already sorted from smallest to largest, where equal values are allowed to sit next to each other. You also get a target value. Exactly one pair of entries in the array adds up to that target, and your job is to locate that pair.
The catch is what you return. Instead of the values themselves, you report the positions of the two entries, counted starting from 1 rather than 0. The smaller position comes first, so the answer is always [index1, index2] with index1 strictly less than index2.
The sorted order is the gift here. It lets you reason about whether a candidate sum is too big or too small and adjust which entries you are looking at, rather than checking every possible pair. The problem also asks for O(1) extra space, which rules out building any auxiliary lookup structure.
The values at positions 1 and 2 are 2 and 7, and 2 + 7 equals 9, so the 1-based answer is [1, 2].
2 at position 1 and 4 at position 3 sum to 6. The matching values sit at the two ends of the array.
With only two values, -1 + 0 already equals the target, so both positions are part of the pair.
Visible test cases
The array being sorted is not just decoration. If a sum of two values is too large, which one should you shrink, and if it is too small, which one should you grow?
Place one marker at the front and one at the back. The front value is the smallest available and the back value is the largest, so their sum brackets the range of reachable sums.
Compare the current sum to the target: too small means move the left marker rightward to a bigger value, too large means move the right marker leftward to a smaller value. Stop when they meet in the middle.
Ignore the sorted order and simply try every pair of positions, checking whether the two values add up to the target.
Since the array is sorted, a sum that is too small can only grow by advancing the low end, and a sum that is too large can only shrink by pulling in the high end, so a single sweep finds the pair.