Skip to main content
TRACK B · ALGORITHMeasy•NOVEMBER 2026•5 MIN

Two Pointers: Converging From Both Ends

Eliminate an entire row or column of pairs on every step by exploiting sorted order.

two pointerssortingarrayspairs3sum

The core idea

When data is sorted, checking the smallest plus largest value (a[l] + a[r]) tells you with certainty which endpoint can never be part of a matching pair.

Watch it run

Step through the trace below (or turn on Predict Mode) to watch the invariant hold at every step.

LOADING TWO POINTERS SIM...

What to say out loud

Canonical Python template

def two_sum_sorted(a: list[int], target: int) -> tuple[int, int] | None:
    l, r = 0, len(a) - 1
    while l < r:
        cur = a[l] + a[r]
        if cur == target:
            return (l, r)
        if cur < target:
            l += 1
        else:
            r -= 1
    return None

Related Lab Entries