← Backtracking

Pattern Recognition Drill

#50 — Generate all subsets

Medium Recursion

The Problem

Generate all subsets (power set) of a list of numbers.

What approach would you use?

Think about it before scrolling down.

Key Signals

Backtracking

For each element, branch: include it or exclude it. Collect results at leaf nodes. O(2^n).

Alt: Bit Manipulation

Iterate from 0 to 2^n-1. Each bitmask represents a subset. Same O(2^n).

Common Trap

Both approaches are O(2^n) — that's unavoidable since there are 2^n subsets.

← #49 #51 →