Problem Description
Given an integer array nums consisting of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. The order of the output does not matter.
Key Insights
- The problem is equivalent to generating the power set of the given set.
- A recursive backtracking approach can be used to explore both including and excluding each element.
- Iterative (bit manipulation) solutions are also possible since there are 2^n subsets in total.
- The maximum array length is small (up to 10), making exponential time solutions feasible.
Space and Time Complexity
Time Complexity: O(2^n * n) - There are 2^n subsets and creating each subset may take up to O(n) time. Space Complexity: O(2^n * n) - Space for storing all subsets. The recursion stack has a maximum depth of O(n).
Solution
We solve the problem using backtracking, where we decide for each element whether to include it in the current subset or not. We recursively build all subsets starting at an empty list and at each step appending the current subset to our result list. We can also use iterative bit manipulation but here we focus on the backtracking approach. This algorithm leverages recursion and a list to maintain the current state, leading to a clear and concise implementation.