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.
Example 1:
Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0] Output: [[],[0]]
Constraints:
1 <= nums.length <= 10-10 <= nums[i] <= 10nums are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of distinct items and you want to discover every possible combination of them, from empty collections to the full set. The brute force strategy involves examining every single item and deciding for each one whether to include it or leave it out, ensuring you map out every conceivable grouping.
Here's how the algorithm would work step-by-step:
def subsets(input_numbers):
# Initialize with an empty group to start building combinations from the ground up
all_subsets = [[]]
for current_number in input_numbers:
# Expand existing groups by adding the current number to each to form new combinations
additional_subsets = []
for existing_subset in all_subsets:
new_subset = existing_subset + [current_number]
additional_subsets.append(new_subset)
# Combine newly formed groups with previous ones to maintain all discovered combinations
all_subsets.extend(additional_subsets)
return all_subsetsTo find every possible group you can make from a collection of items, you can build them step-by-step by deciding whether to include each item or leave it out. The strategy involves exploring all combinations methodically by branching out at each choice point, making sure you capture every unique gathering from the largest group down to nothing at all.
Here's how the algorithm would work step-by-step:
class Solution:
def subsets(self, input_numbers: list[int]) -> list[list[int]]:
all_subsets = []
# We build subsets incrementally by deciding whether to include each element.
def backtrack(start_index: int, current_subset: list[int]):
all_subsets.append(list(current_subset))
for current_index in range(start_index, len(input_numbers)):
current_subset.append(input_numbers[current_index])
# Recursively explore all further combinations with the current item included.
backtrack(current_index + 1, current_subset)
# Backtrack by removing the item to explore paths without it.
current_subset.pop()
backtrack(0, [])
return all_subsets| Case | How to Handle |
|---|---|
| Empty input array nums where length is zero | The backtracking or iterative algorithm should correctly output a list containing only the empty subset. |
| Single element array containing one number | The algorithm should produce exactly two subsets consisting of the empty set and the single element set. |
| Maximum size input array up to the upper constraint limit | Since the output size grows exponentially with two to the power of n, execution will hit time and memory limits for large n. |
| Input array containing negative integers and zero | Standard backtracking treats negative numbers and zeros just like positive numbers without affecting subset generation logic. |
| Recursion depth limits for deeply nested recursive calls | The maximum recursion depth is bounded by the length of the array preventing stack overflow under normal constraints. |
| Order independence in the returned subsets | The problem accepts subsets in any order so standard lexicographical generation satisfies the requirement without extra sorting. |