Practice Discrete Math

Algorithms / Two Pointers Sorted Pairs

Least You Need to Know: Opposite-End Two Pointers and Sorted Pair Search

When data order lets you predict how a sum or comparison changes, **two pointers** can eliminate large parts of the search space without backtracking. This is the interview reason sorted-pair, palindrome, and container-style problems collapse from quadratic scanning to linear passes.

The least you need to know

Key notation

l, r left and right pointers scanning from opposite ends
sorted order monotone order that makes pointer moves predictable
invariant fact preserved after every pointer update

Tiny worked example

  • In a sorted array, compare `a[l] + a[r]` to the target.
  • If the sum is too small, moving `l` right is the only move that can increase it.
  • If the sum is too large, moving `r` left is the only move that can decrease it.
  • That monotone reasoning is why one pass is enough.

Common mistakes

How to recognize this kind of problem

Next recommended lesson

Continue through this topic with Least You Need to Know: Union-Find for Connectivity and Cycles.

Least You Need to Know: Union-Find for Connectivity and Cycles

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

Start practice