AlgoScope

Two Pointers

Two indices moving under a rule do in one pass what a nested loop needs n squared for.

Two pointers, technique, beginner
A pair summing to 14 was found.1031426384115156
A pair summing to 14 was found.1031426384115156

Two Pointers: Two sum on a sorted array

Result, step 5 of 5

Pair found at indices 1 and 5 in 3 steps. One pass, no nested loop.

How two pointers works

Cost

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(1)

Before this

Leads to

Topics that need this one first.

Taught by the same lesson

Two Pointers covers these too, in the same run.