AlgoScope

Opposite Direction Pointers

AlgorithmbeginnerTwo Pointers

Start at both ends and move inward. The comparison decides which one moves.

Decision · step 2 of 5Two Pointers: Two sum on a sorted array
1031426384115156leftright

1 + 15 = 16, too big. Only a smaller right value can help, so right moves down.

Open in the player →or start at step 2

What you will see

Pointers converge from the edges; which one moves depends on the comparison.

How two pointers works →

Cost

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(1)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.

Screen readers: Pointers announce their name and index; each step announces which pointer moved and why.

Reduced motion: Pointers appear at their new index with a brief emphasis instead of gliding.

Before this

Leads to

Topics that need this one first.