Two Pointers
TimeO(n)SpaceO(1)
On a sorted array, start a pointer at each end. Move them inward based on whether the current sum is too small or too large until they meet the target.
Key idea. Two indices converging from the ends of a sorted array. Replaces a nested O(n²) scan with a single linear pass.
1
Loading…
—