Two Pointer Traversal
Two indices walk the array under a rule instead of a nested loop.
Decision · step 2 of 5Two Pointers: Two sum on a sorted array
1 + 15 = 16, too big. Only a smaller right value can help, so right moves down.
What you will see
Two labelled pointers move independently; the rule that moves each one is the lesson.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(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: Each cell is announced as index, value and state; each step's accessibility text states the decision and its effect.
Reduced motion: Swaps and shifts become value crossfades with an outline flash; pointers appear at their destination.
Before this
Taught by the same lesson
Two Pointers covers these too, in the same run.