Skip to main content

Two Pointers

What two pointers means and when two moving indices help reduce comparisons or control a window.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

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:

  1. is there a clear local rule for which side moves?
  2. does the data order help me?
  3. do two indices avoid repeated work here?

Keep exploring