AlgoScope

Opposite Direction Pointers

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

Two pointers, algorithm, beginner
1 plus 15 is 16. More than the target, so the right pointer moves to index 5.1031426384115156leftright
1 plus 15 is 16. More than the target, so the right pointer moves to index 5.1031426384115156leftright

Two Pointers: Two sum on a sorted array

Decision, step 2 of 5

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

How two pointers works

Cost

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

Leads to

Topics that need this one first.

Taught by the same lesson

Two Pointers covers these too, in the same run.