Given an array of integers nums of unique elements. Return all possible subsets (power set) of the array.
Do not include the duplicates in the answer.
Input : nums = [1, 2, 3]
Output : [ [ ] , [1] , [2] , [1, 2] , [3] , [1, 3] , [2, 3] , [1, 2 ,3] ]
Input : nums = [1, 2]
Output : [ [ ] , [1] , [2] , [1, 2] ]
Input : nums = [0]
The power set of a given array is the set of all possible subsets of the array, including the empty set and the set itself. Each element can either be included in a subset or not, resulting in 2n (where n is the number of elements) possible subsets.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function call to get the
Power set of given array */
vector<vector<int>> powerSet(vector<int>& nums) {
// Variable to store size of array
int n = nums.size();
// To store the answer
vector<vector<int>> ans;
/* Variable to store the
count of total susbsets */
int count = (1 << n);
// Traverse for every value
for(int val = 0; val < count; val++) {
// To store the current subset
vector<int> subset;
// Traverse on n bits
for(int i=0; i < n; i++) {
if(val & (1 << i)) {
subset.push_back(nums[i]);
}
}
/* Add the current subset
to final answer */
ans.push_back(subset);
}
// Return stored answer
return ans;
}
};
int main() {
vector<int> nums = {1, 2, 3};
/* Creating an instance of
Solution class */
Solution sol;
/* Function call to get the
Power set of given array */
vector<vector<int>> ans = sol.powerSet(nums);
sort(ans.begin(), ans.end());
// Output
cout << "The power set for the given array is: " << endl;
for(int i=0; i < ans.size(); i++) {
for(int j=0; j < ans[i].size(); j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}
import java.util.*;
class Solution {
/* Function call to get the
Power set of given array */
public List<List<Integer>> powerSet(int[] nums) {
// Variable to store size of array
int n = nums.length;
// To store the answer
List<List<Integer>> ans = new ArrayList<>();
/* Variable to store the
count of total susbsets */
int count = (1 << n);
// Traverse for every value
for (int val = 0; val < count; val++) {
// To store the current subset
List<Integer> subset = new ArrayList<>();
// Traverse on n bits
for (int i = 0; i < n; i++) {
if ((val & (1 << i)) != 0) {
subset.add(nums[i]);
}
}
/* Add the current subset
to final answer */
ans.add(subset);
}
// Return stored answer
return ans;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
/* Creating an instance of
Solution class */
Solution sol = new Solution();
/* Function call to get the
Power set of given array */
List<List<Integer>> ans = sol.powerSet(nums);
ans.sort(Comparator.comparingInt(List::size));
// Output
System.out.println("The power set for the given array is: ");
for (List<Integer> subset : ans) {
for (int num : subset) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
class Solution:
# Function call to get the
# Power set of given array
def powerSet(self, nums):
# Variable to store size of array
n = len(nums)
# To store the answer
ans = []
# Variable to store the
# count of total susbsets
count = (1 << n)
# Traverse for every value
for val in range(count):
# To store the current subset
subset = []
# Traverse on n bits
for i in range(n):
if val & (1 << i):
subset.append(nums[i])
# Add the current subset
# to final answer
ans.append(subset)
# Return stored answer
return ans
# Creating an instance of Solution class
sol = Solution()
# Function call to get the Power set of given array
nums = [1, 2, 3]
ans = sol.powerSet(nums)
ans.sort(key=lambda x: len(x))
# Output
print("The power set for the given array is: ")
for subset in ans:
print(" ".join(map(str, subset)))
class Solution {
/* Function call to get the
Power set of given array */
powerSet(nums) {
// Variable to store size of array
let n = nums.length;
// To store the answer
let ans = [];
/* Variable to store the
count of total susbsets */
let count = (1 << n);
// Traverse for every value
for (let val = 0; val < count; val++) {
// To store the current subset
let subset = [];
// Traverse on n bits
for (let i = 0; i < n; i++) {
if (val & (1 << i)) {
subset.push(nums[i]);
}
}
/* Add the current subset
to final answer */
ans.push(subset);
}
// Return stored answer
return ans;
}
}
// Creating an instance of Solution class
const sol = new Solution();
// Function call to get the Power set of given array
const nums = [1, 2, 3];
const ans = sol.powerSet(nums);
ans.sort((a, b) => a.length - b.length);
// Output
console.log("The power set for the given array is: ");
for (let subset of ans) {
console.log(subset.join(" "));
}
Time Complexity: O(2N*N) (where N is the size of the array) –
Space Complexity: O(2N*N) – Space required to store the power set (approximately).
Q: Why does the power set contain 2^n subsets?
A: Each element in the array has two choices: it can either be included in or excluded from a subset. For n elements, this results in 2^n combinations.
Q: Can subsets be generated in sorted order?
A: Subsets can be sorted by size or lexicographically after generation by applying a sorting function, though the problem doesn't explicitly require this.
Q: How would you extend this problem if duplicates were allowed in the input array?
A: Sort the input array first. During recursion or bitmasking, skip duplicate elements by ensuring the same subset isn't constructed multiple times.
Q: How would you generate subsets of a specific size k?
A: Modify the recursive function or bitmask approach to track the size of the current subset. Only include subsets of size k in the result.