Java Program to Print All Unique Subsets in an Array of Subsets Using Bit Manipulation
Given an integer array of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets and should return the solution in any order.
Input: array = [1,2,3] Output: [,,,[1,2],,[1,3],[2,3],[1,2,3]]
Input: array =  Output: [,]
- Generate all possible binary bitmasks of length n.
- Map a subset to each bitmask
- 1 on the ith position in the bitmask means the presence of nums[i] in the subset
- 0 means its absence.
- Return output list.
[, , , [2, 3], , [1, 3], [1, 2], [1, 2, 3]]
- Time Complexity:- O(N x 2^N) to generate all subsets and then copy them into the output list.
- Space Complexity:- O(N x 2^N) to keep all the subsets of length NN since each of the NN elements could be present or absent.
[, , , [1, 2], , [1, 3], [2, 3], [1, 2, 3]]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.