← All Patterns

Pattern Recognition Drill

#4

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.

Two sum (sorted array)

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 →