Two Pointers Pattern
Two pointers move through a sequence together — from both ends, or at different speeds — to converge on an answer without nested loops. It shines on sorted data and in-place array manipulation.
Typical complexity: O(n) time (after any sort), O(1) space
When to use it
- The input is sorted (or can be sorted cheaply).
- You're looking for a pair/triple meeting a target.
- You need an in-place partition, dedupe, or reversal.
Practice problems
Read the approach or generate an interactive, step-by-step lesson:
Deep dive
Two Pointers Pattern: A Visual Guide to Solving Array Problems
Learn how the two pointers technique works, when to use it, and walk through classic problems like Two Sum II and Container With Most Water.
Master two pointers by doing
codebrew.ai turns each problem into an interactive lesson — trace the algorithm variable by variable until the pattern clicks.
Start a free lesson