numbers is sorted from smallest to largest (equal values may sit next to each other). Find the two different positions whose values add up to target, and return them as [index1, index2].
Positions here count from 1, not 0, and index1 < index2. You can't use one position twice, though two positions may hold the same value. Exactly one pair of positions works.
Aim for O(1) extra memory: the order of numbers already holds everything you need. The judge only checks the answer, so it can't tell how much memory you used.
6 and 9 are the third and fourth numbers, and 6 + 9 = 15. No other pair reaches 15.
-4 + 0 = -4. Negative numbers and zero work like any others.
The two 3s are different positions, so together they make 6.
Constraints
2 <= numbers.length <= 3 · 10⁴-1000 <= numbers[i] <= 1000numbers is sorted in non-decreasing order.-1000 <= target <= 1000index1 < index2 has numbers[index1] + numbers[index2] == target.Start with the smallest and the largest number. Too small a sum can only grow by dropping the smallest number, and too large a sum can only shrink by dropping the largest. Neither step can throw away part of the answer.
Add the smallest number to the largest. Because numbers is sorted, that one sum tells you about far more than one pair.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium