Quant Memo
Coding/●●●●●

Two-sum in a sorted array, in one pass

You're given an array nums sorted in ascending order and a target t. Find two positions whose values add up to exactly t.

nums = [2, 3, 5, 8, 11], t = 14
-> (1, 4)      # 3 + 11 = 14

Return a pair of indices that sums to t, in O(n)O(n) time and O(1)O(1) space. Be ready to justify why your pointer move never throws away a valid pair.

Show a hint

Start with the smallest and the largest value. If their sum is too small, which of the two can you never fix by keeping it?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions