Leetcode 167: Two Sum II — Input Array is Sorted

Pierre-Marie Poitevin
2 min readApr 23, 2024

In this exercise, we need to find the unique pair of indices [x, y] such that numbers[x] + numbers[y] == target, with numbers a sorted array.

a. A little bit of Math

We need to use the sorted array in order not to check every single pair. How do we eliminate pairs?

If numbers[i] + numbers[j] < target with i < j, then numbers[i] + numbers[k] < target for all k ≤ j, and so all these pairs don’t need to be checked. We have a similar result with numbers[i] + numbers[j] >target.

--

--