Arrays and two pointers

Two techniques that solve many array questions in O(n).

Two pointers

On a sorted array, move one index from each end. This finds a pair with a given sum without checking every pair.

Expected output
(1, 9)

Sliding window

For a best stretch of fixed length, update the total as the window moves instead of adding it up again.

Expected output
9
โ† Big O: measuring speedStacks and queues โ†’