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.

Note: The power set of a set is the set of all its subsets, including the empty set and the set itself.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
💡 Note: All possible combinations of including/excluding each element. Empty set and full set are both included.
Example 2 — Two Elements
$ Input: nums = [0]
Output: [[],[0]]
💡 Note: With one element, we have 2¹ = 2 subsets: empty set and the single element.
Example 3 — Larger Array
$ Input: nums = [1,2]
Output: [[],[1],[2],[1,2]]
💡 Note: With two elements, we have 2² = 4 subsets representing all include/exclude combinations.

Constraints

  • 1 ≤ nums.length ≤ 10
  • -10 ≤ nums[i] ≤ 10
  • All the numbers of nums are unique

Visualization

Tap to expand
Subsets Problem: Generate Power SetInput Array:123All Possible Subsets:[] (empty set)[1][2][3][1,2][1,3][2,3][1,2,3] (full set)Total: 2³ = 8 subsetsGeneratePower Set: Collection of All Subsets
Understanding the Visualization
1
Input
Array of unique integers [1,2,3]
2
Process
Generate all combinations of include/exclude
3
Output
2^n subsets including empty set
Key Takeaway
🎯 Key Insight: Each element has 2 choices (include/exclude), leading to 2^n total combinations
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28
508.9K Views
High Frequency
~15 min Avg. Time
15.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen