Browse Curriculum
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
Watch how the tree expands. Each level represents an index in nums.
Try clicking 'Randomize' to see how the tree changes with different input arrays.
ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
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.
