Subsets - Problem
Generate All Possible Subsets (Power Set)
Given an integer array
A subset is any combination of elements from the original array, including the empty set and the array itself. The order of subsets in the result doesn't matter, but each subset should appear exactly once.
Example: For array
๐ก Fun fact: An array of n elements always has exactly 2n subsets!
Given an integer array
nums containing unique elements, your task is to return all possible subsets of the array, also known as the power set.A subset is any combination of elements from the original array, including the empty set and the array itself. The order of subsets in the result doesn't matter, but each subset should appear exactly once.
Example: For array
[1,2,3], the power set contains 8 subsets: [], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]๐ก Fun fact: An array of n elements always has exactly 2n subsets!
Input & Output
example_1.py โ Basic Case
$
Input:
[1,2,3]
โบ
Output:
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
๐ก Note:
For 3 elements, we get 2ยณ = 8 subsets. Each element can either be included or excluded from each subset.
example_2.py โ Single Element
$
Input:
[0]
โบ
Output:
[[],[0]]
๐ก Note:
With one element, we have 2ยน = 2 subsets: the empty set and the set containing the single element.
example_3.py โ Empty Array Edge Case
$
Input:
[]
โบ
Output:
[[]]
๐ก Note:
An empty array has only one subset: the empty set itself. This follows 2โฐ = 1.
Constraints
- 1 โค nums.length โค 10
- -10 โค nums[i] โค 10
- All the numbers of nums are unique
Visualization
Tap to expand
Understanding the Visualization
1
Binary Choice
Each element has 2 options: include or exclude
2
Systematic Generation
Use either backtracking or bit manipulation to explore all 2^n possibilities
3
Complete Power Set
Collect all generated subsets from empty set to full set
Key Takeaway
๐ฏ Key Insight: Every element has exactly 2 choices (include/exclude), giving us 2^n total combinations. Both bit manipulation and backtracking systematically explore all possibilities.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code