← Two Pointers

Pattern Recognition Drill

#4 — Two sum (sorted array)

Easy Arrays & Strings

The Problem

Given sorted array a and target t, return indices of the pair that sums to t.

What approach would you use?

Think about it before scrolling down.

Key Signals

Two Pointers

Start from both ends. If sum < target, move left up; if sum > target, move right down. O(n).

Alt: Hash Map

Works (O(n)) but wastes the sorted property. Interviewer wants you to use the sort.

Common Trap

Binary search for each element is O(n log n) — works but two pointers is cleaner and O(n).

← #3 #5 →