Skip to content

Two Pointers

Index movement with a proof

“Two pointers” is a family of array/string algorithms where two indices move through the input together, maintaining an invariant that lets you prune or conclude without re-scanning. The pattern most often works on sorted data or on palindrome-shaped symmetry; it converts many O(n2)O(n²) brute forces into O(n)O(n).

Three pointer movements cover almost all problems:

  • Converging: one pointer from each end, move the one whose value fails an invariant inward.
  • Same-direction: both pointers advance left-to-right; “slow” marks a write position, “fast” marks a read position. Often overlaps with sliding window.
  • Trailing: a second pointer lags behind the first by a fixed offset (Remove Nth From End of a linked list).

Two pointers is not “use two variables.” It is a pruning argument. When you move one pointer, you are claiming that every skipped pair or state cannot improve the answer. Sorted order, symmetry, or a monotonic boundary is what makes that claim true.

How to choose the movement

Prompt shapeMovementWhy it works
Palindrome or mirror comparisonConverging endsOnly paired characters matter.
Sorted array, find pair with target sumConverging endsToo small means move left up. Too large means move right down.
3Sum or n-sumFixed anchor plus converging endsSorting removes duplicates and lets each inner search be linear.
Max area between two wallsConverging ends, move shorter wallThe shorter wall limits area, so keeping it cannot help.
Trapping rain waterConverging ends with running maximaThe lower side determines the safe water level to finalize.
In-place compaction or compressionSame-direction read/write pointersOne pointer reads all input. The other writes the kept form.

Problems

  1. 125. Valid Palindrome (Easy)
  2. 167. Two Sum II, Input Array Is Sorted (Medium)
  3. 15. 3Sum (Medium)
  4. 11. Container With Most Water (Medium)
  5. 42. Trapping Rain Water (Hard)

Bonus problems

Key patterns unlocked here

  • Symmetric check with converging pointers: Valid Palindrome.
  • Sorted-array complement search: Two Sum II. The pattern that generalizes to 3Sum and 4Sum.
  • Sort + fixed anchor + two pointers: 3Sum; the bread and butter of n-sum problems.
  • Greedy movement on the shorter side: Container With Most Water; the “why two pointers work” proof case.
  • Bidirectional max tracking: Trapping Rain Water (classic two-pointer alternative to prefix/suffix-max arrays).

Common mistakes

  • Using two pointers before sorting when the proof depends on sorted order.
  • Moving both pointers after finding a match without skipping duplicates in 3Sum-style problems.
  • Moving the taller wall in Container With Most Water. The shorter wall is the limiting factor.
  • Confusing sliding window with two pointers. Sliding window maintains a contiguous valid range. Two pointers often compare endpoints or maintain read/write positions.
  • Forgetting that pointer movement is the proof. If you cannot explain what was ruled out, the solution is probably accidental.

How the problems fit together

Valid Palindrome is the clean symmetry case. Two Sum II is the clean sorted-order case. 3Sum combines sorting, duplicate skipping, a fixed anchor, and an inner two-pointer search.

Container With Most Water is the best proof exercise in the group because every move discards many possible pairs. Trapping Rain Water is the advanced endpoint problem: each side carries the best boundary seen so far.

The bonus problems show the same idea in mutation tasks. Rotate Array uses reversal boundaries. String Compression uses read/write pointers so the output fits back into the input array.