← Hash Map

Pattern Recognition Drill

#23 — Two sum (unsorted)

Easy Hash Maps

The Problem

Given unsorted 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

Hash Map

For each element x, check if (target - x) is in the map. Insert x after checking. O(n).

Alt: Sorting

Sort + two pointers is O(n log n) but loses original indices — need extra bookkeeping.

Common Trap

Brute force O(n²) checks all pairs. The hash map complement lookup is the key insight.

← #22 #24 →