March 24
Two Pointers
What two pointers means and when two moving indices help reduce comparisons or control a window.
What it is
Two pointers is a technique where you use two indices or references moving through the same structure.
They may:
- move toward each other
- move together at different speeds
- define the edges of a window
When to use it
It often fits when:
- the order of the structure matters
- you want to avoid repeated comparisons
- you can decide which side moves by looking at the current state
Common cases:
- sorted arrays
- palindrome checks
- windows with left and right boundaries
Common mistake
The classic mistake is memorizing the label instead of the movement rule.
If you do not know why the left or right pointer moves, the technique becomes guesswork.
Better question
Instead of asking “is this two pointers?”, ask:
- is there a clear local rule for which side moves?
- does the data order help me?
- do two indices avoid repeated work here?
Share this page
Copy the link manually from the field below.