Subsets (Power Set)
Subsets (Power Set) is LeetCode 78 (Medium). Given an array nums of distinct integers, return every subset of it, from the empty list to the whole array. This collection is called the power set, and it may be returned in any order.
- n elements give 2^n subsets. With n at most 10, the answer has at most 1,024 lists.
- All elements are different, so no two subsets can come out equal.
- 1 <= nums.length <= 10
- -10 <= nums[i] <= 10
- All the numbers of nums are unique.
Intuition
Walk the array from left to right and make one yes/no decision per element: put it in the current subset, or leave it out. A full set of decisions describes exactly one subset, and two different sets of decisions always give different subsets.
Draw the decisions as a binary tree. Level i decides nums[i]: the left branch takes it and the right branch skips it. After n levels every element is decided, so each of the 2^n leaves is one finished subset.
Backtracking walks this tree with one shared list, path. It appends before the take branch, removes the element before the skip branch, and saves a copy at every leaf.
This is the standard power set backtracking pattern: every element is independently in or out, and you must list every outcome. Combinations, Subsets II and many "choose some items" problems are this tree with extra rules.
Approach
Before reading on, draw the tree of choices for [1, 2] by hand: which branch adds 2, and what must path look like when you come back up? Aim for O(n · 2^n) time.
Two ways to solve it
Decide each element in turn with one shared path, and save a copy at each leaf.
- Extends: the same shape solves Subsets II and Combination Sum.
- Clear trace: one branch per decision.
The usual interview answer.
Count mask from 0 to 2^n − 1 and read bit j as "take nums[j]".
- No recursion: two plain loops.
- Limit: only works while 2^n fits in an integer, which is fine for n ≤ 10.
Both produce each subset once and pay up to n to write it out. Backtracking carries over to harder variants, so the steps, code and live demo follow it. The bitmask version comes after the demo.
Keep one list for the current subset
path holds the elements chosen so far and res collects finished subsets. Sharing one list means no new list is built at each call, only one copy per finished subset.
Take the element and go deeper
At index i, append nums[i] to path and call dfs(i + 1). Every subset found below this call contains nums[i], because it stays in path until that call returns.
Undo, then skip it
Pop nums[i] and call dfs(i + 1) again. The path is back to what it was before the take, so this second branch finds every subset without nums[i].
Save a copy at the end of the array
When i == len(nums), every element has been decided, so the path is one complete subset. Append a copy of it to res and return to the caller.
Code in Python, C++ and Java
Bitmask counting
Another way to generate all subsets is to count. Every number from 0 to 2^n − 1 has n bits, and bit j says whether nums[j] is in. Mask 5 is binary 101, so with [1, 2, 3] it picks 1 and 3. The 2^n masks are exactly the 2^n subsets.
Common pitfalls
Saving the shared list instead of a copy
res.append(path)
res.append(path[:])
Every entry of res then points at the same list, which is empty once the search finishes. For [1, 2, 3] the result is eight empty lists. In Java, add new ArrayList<>(path); C++ push_back(path) already copies.
Skipping without undoing the take
path.append(nums[i]) dfs(i + 1) dfs(i + 1)
path.append(nums[i]) dfs(i + 1) path.pop() dfs(i + 1)
The skip branch then still carries nums[i]. For [1] the result is [[1], [1]] instead of [[1], []], and longer inputs collect ever-growing lists.
Complexity
path are stored, each at most n deep.Subsets (Power Set) FAQ
Why is my output in a different order from the expected one?
Any order is accepted. Taking before skipping puts [1, 2, 3] first and [] last; skipping first reverses that. The judge compares the collections, not the positions.
Can I just use itertools.combinations?
Yes: chaining combinations(nums, r) for every r from 0 to n gives the power set in the same O(n · 2^n). Interviewers usually want the recursion, though, because Subsets II and Combination Sum build on it.
What changes when the array has duplicates?
Taking either of two equal values gives the same subset, so the tree produces repeats. Subsets II sorts the array and skips a value that equals the one tried just before it at the same level.