AlgoScope

Two Pointer Traversal

Two indices walk the array under a rule instead of a nested loop.

Array algorithms and techniques, technique, beginner
Looking for a pair summing to 14 in a sorted array, with a pointer at each end.1031426384115156leftright
Looking for a pair summing to 14 in a sorted array, with a pointer at each end.1031426384115156leftright

Two Pointers: Two sum on a sorted array

Start, step 1 of 5

Find two values that add up to 14. The array is sorted, so start at both ends and let the sum tell you which pointer to move.

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.