Subsets
Generate all 2^n subsets of an array of distinct integers using state-space exploration with backtracking.
01The problem
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
| Input | Output | Why |
|---|---|---|
nums = [1, 2, 3] | [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]] | All 8 unique subsets |
nums = [0] | [[], [0]] | Empty subset and single element subset |
Constraints: 1 <= nums.length <= 10, -10 <= nums[i] <= 10, all numbers are unique.
02How to spot it
- The problem asks for all combinations of any length (subsets).
- For an array of size
n, there are2^ntotal subsets. - Every node in the decision tree represents a valid subset: the empty set, subsets of size 1, 2, up to
n. - By maintaining a
startpointer, each step only considers numbers to the right, ensuring combinations never repeat in reverse order.
So: backtracking with start index.
03Pseudocode
build(start, cur):
record a copy of cur
for i from start to n - 1:
cur.add(nums[i])
build(i + 1, cur)
cur.removeLast()- At every recursion step, save a copy of
curto the result list. - Loop
ifromstartton - 1: addnums[i], recurse withstart = i + 1. - Backtrack: remove
nums[i]fromcur.
04Watch it work
nums
current subset
total subsets recorded
0
Generate all subsets (the power set) by exploring choices with backtracking.
Pseudocode
- build(start, cur):
- record a copy of cur
- for i from start to n - 1:
- cur.add(nums[i])
- build(i + 1, cur)
- cur.removeLast()
Variables
- nums
- 1,2,3
- count
- 0
05Step-by-step dry run
- 1
nums
102132current subset
total subsets recorded
0
Generate all subsets (the power set) by exploring choices with backtracking.
nums = 1,2,3count = 0
- 2
nums
102132icurrent subset
total subsets recorded
1
Record current subset: []. Total recorded: 1.
subset = total = 1
- 3
nums
102132icurrent subset
10total subsets recorded
1
Choose nums[0] = 1. Current subset: [1]. Recurse with start = 1.
i = 0added = 1cur = 1
- 4
nums
102132icurrent subset
10total subsets recorded
2
Record current subset: [1]. Total recorded: 2.
subset = 1total = 2
- 5
nums
102132icurrent subset
1021total subsets recorded
2
Choose nums[1] = 2. Current subset: [1, 2]. Recurse with start = 2.
i = 1added = 2cur = 1,2
- 6
nums
102132icurrent subset
1021total subsets recorded
3
Record current subset: [1, 2]. Total recorded: 3.
subset = 1,2total = 3
- 7
nums
102132icurrent subset
102132total subsets recorded
3
Choose nums[2] = 3. Current subset: [1, 2, 3]. Recurse with start = 3.
i = 2added = 3cur = 1,2,3
- 8
nums
102132current subset
102132total subsets recorded
4
Record current subset: [1, 2, 3]. Total recorded: 4.
subset = 1,2,3total = 4
- 9
nums
102132icurrent subset
1021total subsets recorded
4
Backtrack: removed 3 from subset. Restore to [1, 2].
popped = 3cur = 1,2
- 10
nums
102132icurrent subset
10total subsets recorded
4
Backtrack: removed 2 from subset. Restore to [1].
popped = 2cur = 1
- 11
nums
102132icurrent subset
1031total subsets recorded
4
Choose nums[2] = 3. Current subset: [1, 3]. Recurse with start = 3.
i = 2added = 3cur = 1,3
- 12
nums
102132current subset
1031total subsets recorded
5
Record current subset: [1, 3]. Total recorded: 5.
subset = 1,3total = 5
- 13
nums
102132icurrent subset
10total subsets recorded
5
Backtrack: removed 3 from subset. Restore to [1].
popped = 3cur = 1
- 14
nums
102132icurrent subset
total subsets recorded
5
Backtrack: removed 1 from subset. Restore to [].
popped = 1cur =
- 15
nums
102132icurrent subset
20total subsets recorded
5
Choose nums[1] = 2. Current subset: [2]. Recurse with start = 2.
i = 1added = 2cur = 2
- 16
nums
102132icurrent subset
20total subsets recorded
6
Record current subset: [2]. Total recorded: 6.
subset = 2total = 6
- 17
nums
102132icurrent subset
2031total subsets recorded
6
Choose nums[2] = 3. Current subset: [2, 3]. Recurse with start = 3.
i = 2added = 3cur = 2,3
- 18
nums
102132current subset
2031total subsets recorded
7
Record current subset: [2, 3]. Total recorded: 7.
subset = 2,3total = 7
- 19
nums
102132icurrent subset
20total subsets recorded
7
Backtrack: removed 3 from subset. Restore to [2].
popped = 3cur = 2
- 20
nums
102132icurrent subset
total subsets recorded
7
Backtrack: removed 2 from subset. Restore to [].
popped = 2cur =
- 21
nums
102132icurrent subset
30total subsets recorded
7
Choose nums[2] = 3. Current subset: [3]. Recurse with start = 3.
i = 2added = 3cur = 3
- 22
nums
102132current subset
30total subsets recorded
8
Record current subset: [3]. Total recorded: 8.
subset = 3total = 8
- 23
nums
102132icurrent subset
total subsets recorded
8
Backtrack: removed 3 from subset. Restore to [].
popped = 3cur =
- 24
total subsets
8
Completed: all 2^3 = 8 subsets generated.
count = 8
06Java solution
import java.util.ArrayList;
import java.util.List;
public class Subsets {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
build(nums, 0, new ArrayList<>(), res);
return res;
}
private static void build(int[] nums, int start, List<Integer> cur, List<List<Integer>> res) {
res.add(new ArrayList<>(cur));
for (int i = start; i < nums.length; i++) {
cur.add(nums[i]);
build(nums, i + 1, cur, res);
cur.remove(cur.size() - 1);
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
System.out.println(subsets(nums));
}
}[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
07Why it works
The recursion tree spans exactly 2^n states.
Because each element can either be chosen or skipped:
- Passing
i + 1to the next recursive frame guarantees that each number is selected at most once. - Iterating strictly forward (
i >= start) ensures that if[1, 2]was formed,[2, 1]is never explored. - Making a copy
new ArrayList<>(cur)captures the subset snapshot beforecuris altered by subsequent branch iterations.
08Time and space
- Time complexity:
O(n * 2^n). There are2^nsubsets, and copying each subset takesO(n)time. - Space complexity:
O(n)auxiliary space for the recursion call stack andcurlist.
09Edge cases
nums.length == 1: Generates[[], [x]].- Empty input
[]: Generates[[]].
10Common mistakes
- Writing
res.add(cur)instead ofres.add(new ArrayList<>(cur)). Storing the referencecurdirectly means every entry inrespoints to the exact same list object, which empties out when backtracking finishes. - Passing
start + 1instead ofi + 1. Passingstart + 1causes infinite loops or repeats numbers from earlier loop iterations.
11Other approaches
| Approach | Time | Space | Notes |
|---|---|---|---|
| Backtracking (start pointer) | O(n * 2^n) | O(n) stack | Standard idiomatic solution |
| Bitmasking | O(n * 2^n) | O(1) extra | Iterates integers 0 to 2^n - 1 |
| Cascading | O(n * 2^n) | O(n) | Iteratively adds nums[i] to all existing subsets |
12In the interview
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why must we copy cur with new ArrayList<>(cur) when adding to res?
2. How many total subsets are generated for an array of length n?
14Solve it yourself
Try implementing the bit manipulation version:
- Loop
maskfrom0to(1 << n) - 1. - Check
if ((mask & (1 << j)) != 0)to addnums[j].