Two pointers · pair with a target sum
Find two different positions whose values add up to the target. Start at the ends.
The values are in increasing order and may repeat. Each cell adds the value of its row to the value of its column.
If the sum is too small, even the largest partner cannot help the left value. If it is too large, even the smallest partner cannot help the right value.
The pointers move inward. For n values there are at most n − 1 pair comparisons, using constant extra space.