Why it works
- The numbers are sorted, so moving left to the right can only make the sum bigger, and moving right to the left can only make it smaller.
- A move rules a number out for good. If the sum is too small, the left number with any other number still in play is no bigger, so it has no partner. Too big: the same for the right number.
- Unlike the card, the answer is positions counted from 1, not indices: add 1 to each pointer.
Complexity
Time O(n): each step moves one pointer inwards, so there are at most n − 1 steps. Memory O(1): only two pointers and a sum.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗