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.
Illustration:
Input: array = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Input: array = [0] Output: [[],[0]]
Approach 1
Algorithm
- 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.
Implementation:
Example
Java
// Java Program to Print all unique subsets in an array of // subsets using bit manipulation // Importing input output classes import java.io.*; // Importing utility classes import java.util.*; // Main class class GFG { // Method 1 // Helper method static List<List<Integer> > subsets( int [] nums) { List<List<Integer> > output = new ArrayList<>(); int n = nums.length; for ( int i = ( int )Math.pow( 2 , n); i < ( int )Math.pow( 2 , n + 1 ); ++i) { // Generate bitmask, from 0..00 to 1..11 String bitmask = Integer.toBinaryString(i).substring( 1 ); // Appending subset corresponding to that // bitmask List<Integer> curr = new ArrayList<>(); for ( int j = 0 ; j < n; ++j) { if (bitmask.charAt(j) == '1' ) // Adding it to subset curr.add(nums[j]); } // Adding the subset output.add(curr); } return output; } // Method 2 // Main driver method public static void main(String[] args) { // Custom input integer array int arr[] = { 1 , 2 , 3 }; // Calling method 1 in main() method List<List<Integer> > output = subsets(arr); // Printing all unique subsets in an array System.out.println(output); } } |
Output
[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
Complexity Analysis:
- 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.
Approach 2:
Example
Java
// Java Program to Print all unique subsets in an array of // subsets using bit manipulation // Importing input output classes import java.io.*; // Importing utility classes import java.util.*; // Main class class GFG { // Method 1 static List<List<Integer> > subsets( int [] nums) { // Creating List class object // Declaring. object of integer List List<List<Integer> > output = new ArrayList<>(); int n = nums.length; // Increase the size by using left shift (1 * 2^n) int size = 1 << n; for ( int i = 0 ; i < size; i++) { List<Integer> curr = new ArrayList<>(); for ( int j = 0 ; j < n; j++) { // right shift i and j i.e. i/2^j if (((i >> j) & 1 ) == 1 ) { // Add it to subset curr.add(nums[j]); } } // Adding the subset output.add(curr); } return output; } // Method 2 // Main driver method public static void main(String[] args) { // Custom input array int arr[] = { 1 , 2 , 3 }; // Calling method 1 in main() method List<List<Integer> > output = subsets(arr); // Print all unique subsets in an array System.out.println(output); } } |
Output
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
Please Login to comment...