Learning pathsA
Array and string patterns

Two pointers

Use order to eliminate impossible candidates.

Understand the problem

Maintain two positions and an invariant explaining which pointer may move. On a sorted array, a sum below the target requires a larger left value; a sum above the target requires a smaller right value.

Make it concrete

For [1, 3, 5, 8] and target 9, compare 1+8 first. The match is found without checking every pair.

Trade-offs and pitfalls

Sorting costs O(n log n) and can destroy original index order.

Check your understanding

Solve sorted two-sum in O(n) time and O(1) extra space.

Practice this topic

Your study notes