Quant Memo
Coding/●●●●●

Closest pair sum in a sorted array

You're given a sorted array nums and a target t. Among all pairs of distinct positions, find the one whose sum is closest to t.

nums = [1, 3, 4, 7, 10], t = 15
-> 14         # 4 + 10 is closest to 15

Return the closest achievable sum. Aim for O(n)O(n) time and O(1)O(1) space, and justify why the pointer you move never skips the best pair.

Show a hint

If the current sum is below the target, only one of the two pointers can raise it. Moving the other pointer strictly moves you away.

Your answer

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

More Coding questions