Backtracking
Medium
Subsets (Power Set)
Generate all possible subsets of a set of unique integers.
10 min read
Solve on LeetCodeProblem Understanding
Given an integer array nums of unique elements, return all possible subsets (the power set).
Example: nums = [1, 2, 3]
- Output:
[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]
There should be $2^N$ subsets.
Algorithm Strategy
We can use the Choose/Not Choose pattern.
For each element in the array, we have exactly two choices:
- Include it in the current subset.
- Exclude it from the current subset.
We iterate from index 0 to n. At each index, we branch into these two possibilities.
Interactive Visualization
Step 1 / 1
Empty Heap
Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution
- Complexity Analysis
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
