Two Pointers
Two indices moving under a rule do in one pass what a nested loop needs n squared for.
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; the rule that moves each is the whole 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: 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.
Taught by the same lesson
Two Pointers covers these too, in the same run.