Skip to content
Veltoryx Technologies
FreeProblem 74● MediumLeetCode #78 ↗3 min readUpdated 30 October 2026

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.

InputOutputWhy
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 are 2^n total 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 start pointer, each step only considers numbers to the right, ensuring combinations never repeat in reverse order.

So: backtracking with start index.

03Pseudocode

text
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()
  1. At every recursion step, save a copy of cur to the result list.
  2. Loop i from start to n - 1: add nums[i], recurse with start = i + 1.
  3. Backtrack: remove nums[i] from cur.

04Watch it work

Visualizer · step through the algorithmnums = [1,2,3]

nums

10
21
32

current subset

total subsets recorded

0

Generate all subsets (the power set) by exploring choices with backtracking.

Pseudocode

  1. build(start, cur):
  2. record a copy of cur
  3. for i from start to n - 1:
  4. cur.add(nums[i])
  5. build(i + 1, cur)
  6. cur.removeLast()

Variables

nums
1,2,3
count
0
step 1 / 24

05Step-by-step dry run

  1. 1

    nums

    10
    21
    32

    current subset

    total subsets recorded

    0

    Generate all subsets (the power set) by exploring choices with backtracking.

    nums = 1,2,3count = 0

  2. 2

    nums

    10
    21
    32
    i

    current subset

    total subsets recorded

    1

    Record current subset: []. Total recorded: 1.

    subset = total = 1

  3. 3

    nums

    10
    21
    32
    i

    current subset

    10

    total subsets recorded

    1

    Choose nums[0] = 1. Current subset: [1]. Recurse with start = 1.

    i = 0added = 1cur = 1

  4. 4

    nums

    10
    21
    32
    i

    current subset

    10

    total subsets recorded

    2

    Record current subset: [1]. Total recorded: 2.

    subset = 1total = 2

  5. 5

    nums

    10
    21
    32
    i

    current subset

    10
    21

    total subsets recorded

    2

    Choose nums[1] = 2. Current subset: [1, 2]. Recurse with start = 2.

    i = 1added = 2cur = 1,2

  6. 6

    nums

    10
    21
    32
    i

    current subset

    10
    21

    total subsets recorded

    3

    Record current subset: [1, 2]. Total recorded: 3.

    subset = 1,2total = 3

  7. 7

    nums

    10
    21
    32
    i

    current subset

    10
    21
    32

    total subsets recorded

    3

    Choose nums[2] = 3. Current subset: [1, 2, 3]. Recurse with start = 3.

    i = 2added = 3cur = 1,2,3

  8. 8

    nums

    10
    21
    32

    current subset

    10
    21
    32

    total subsets recorded

    4

    Record current subset: [1, 2, 3]. Total recorded: 4.

    subset = 1,2,3total = 4

  9. 9

    nums

    10
    21
    32
    i

    current subset

    10
    21

    total subsets recorded

    4

    Backtrack: removed 3 from subset. Restore to [1, 2].

    popped = 3cur = 1,2

  10. 10

    nums

    10
    21
    32
    i

    current subset

    10

    total subsets recorded

    4

    Backtrack: removed 2 from subset. Restore to [1].

    popped = 2cur = 1

  11. 11

    nums

    10
    21
    32
    i

    current subset

    10
    31

    total subsets recorded

    4

    Choose nums[2] = 3. Current subset: [1, 3]. Recurse with start = 3.

    i = 2added = 3cur = 1,3

  12. 12

    nums

    10
    21
    32

    current subset

    10
    31

    total subsets recorded

    5

    Record current subset: [1, 3]. Total recorded: 5.

    subset = 1,3total = 5

  13. 13

    nums

    10
    21
    32
    i

    current subset

    10

    total subsets recorded

    5

    Backtrack: removed 3 from subset. Restore to [1].

    popped = 3cur = 1

  14. 14

    nums

    10
    21
    32
    i

    current subset

    total subsets recorded

    5

    Backtrack: removed 1 from subset. Restore to [].

    popped = 1cur =

  15. 15

    nums

    10
    21
    32
    i

    current subset

    20

    total subsets recorded

    5

    Choose nums[1] = 2. Current subset: [2]. Recurse with start = 2.

    i = 1added = 2cur = 2

  16. 16

    nums

    10
    21
    32
    i

    current subset

    20

    total subsets recorded

    6

    Record current subset: [2]. Total recorded: 6.

    subset = 2total = 6

  17. 17

    nums

    10
    21
    32
    i

    current subset

    20
    31

    total subsets recorded

    6

    Choose nums[2] = 3. Current subset: [2, 3]. Recurse with start = 3.

    i = 2added = 3cur = 2,3

  18. 18

    nums

    10
    21
    32

    current subset

    20
    31

    total subsets recorded

    7

    Record current subset: [2, 3]. Total recorded: 7.

    subset = 2,3total = 7

  19. 19

    nums

    10
    21
    32
    i

    current subset

    20

    total subsets recorded

    7

    Backtrack: removed 3 from subset. Restore to [2].

    popped = 3cur = 2

  20. 20

    nums

    10
    21
    32
    i

    current subset

    total subsets recorded

    7

    Backtrack: removed 2 from subset. Restore to [].

    popped = 2cur =

  21. 21

    nums

    10
    21
    32
    i

    current subset

    30

    total subsets recorded

    7

    Choose nums[2] = 3. Current subset: [3]. Recurse with start = 3.

    i = 2added = 3cur = 3

  22. 22

    nums

    10
    21
    32

    current subset

    30

    total subsets recorded

    8

    Record current subset: [3]. Total recorded: 8.

    subset = 3total = 8

  23. 23

    nums

    10
    21
    32
    i

    current subset

    total subsets recorded

    8

    Backtrack: removed 3 from subset. Restore to [].

    popped = 3cur =

  24. 24

    total subsets

    8

    Completed: all 2^3 = 8 subsets generated.

    count = 8

06Java solution

Subsets.java
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));
    }
}
Expected output
[[], [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 + 1 to 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 before cur is altered by subsequent branch iterations.

08Time and space

  • Time complexity: O(n * 2^n). There are 2^n subsets, and copying each subset takes O(n) time.
  • Space complexity: O(n) auxiliary space for the recursion call stack and cur list.

09Edge cases

  • nums.length == 1: Generates [[], [x]].
  • Empty input []: Generates [[]].

10Common mistakes

  • Writing res.add(cur) instead of res.add(new ArrayList<>(cur)). Storing the reference cur directly means every entry in res points to the exact same list object, which empties out when backtracking finishes.
  • Passing start + 1 instead of i + 1. Passing start + 1 causes infinite loops or repeats numbers from earlier loop iterations.

11Other approaches

ApproachTimeSpaceNotes
Backtracking (start pointer)O(n * 2^n)O(n) stackStandard idiomatic solution
BitmaskingO(n * 2^n)O(1) extraIterates integers 0 to 2^n - 1
CascadingO(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. 1. Why must we copy cur with new ArrayList<>(cur) when adding to res?

  2. 2. How many total subsets are generated for an array of length n?

14Solve it yourself

Try implementing the bit manipulation version:

  1. Loop mask from 0 to (1 << n) - 1.
  2. Check if ((mask & (1 << j)) != 0) to add nums[j].