Two Sum II - Input Array Is Sorted

mediumTwo Pointerstwo pointers on sorted arraySolve on LeetCode#167

Part of Practice: Converging Pairs

You are given an array of integers sorted in non-decreasing order and a target value. Find the two different elements that add up to the target and return their positions as 1-indexed values (the first element is position 1, not 0). Exactly one answer exists.

The twist versus the classic Two Sum: the required solution must use only constant extra memory, which rules out the hash map.

Examples

Input:
numbers = [2, 7, 11, 15], target = 9
Output:
[1, 2]

2 + 7 = 9. The answer is 1-indexed, so positions 1 and 2, not 0 and 1.

Input:
numbers = [2, 3, 4], target = 6
Output:
[1, 3]

2 + 4 = 6.

Input:
numbers = [-1, 0], target = -1
Output:
[1, 2]

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.