Lesson

Two Pointers

Move two indices cooperatively to shrink search space in linear time.

Easy → MediumTime O(n)Space O(1)
Practice problems
Loading visualization…

Loading solution…

When it applies

Sorted arrays, palindromes, partitioning, and problems where moving one pointer dominates the other based on a comparison.

Teach with Viz

Open the Two Pointers studio lesson. Watch L and R converge while the code line highlight tracks the branch you take on each sum comparison.

Proof sketch

After sorting, if A[L]+A[R] is too small, any pair using L with a smaller R is worse — so advance L. Symmetric argument for R.

Pitfalls

  • !Forgetting to sort when required
  • !Infinite loops when neither pointer moves
  • !Overlapping pointers incorrectly

Practice from this topic

Showing 24 of the related problems in the 1000-question bank.

View all →

Got the lesson?

Mark Two Pointers as understood when the ideas stick.