Java Program to Generate All Possible Subsets using Binary Counting Method

This is a java program to generate and print all possible subsets using the method of Binary Counting method. The generations of subsets are done using binary numbers. Let there be 3 elements in the set, we generate binary equivalent of 2^3 = 8 numbers(0-7), where each bit in a number represents the presence/absence of element in the subset. The element is present if bit is 1, absent otherwise. 010 – only second element is present in the subset.

Here is the source code of the Java Program to Implement the Binary Counting Method to Generate Subsets of a Set. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to generate all subsets of given set of numbers using binary counting method
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Binary_Counting_Subsets 
  6. {
  7.     public static int[] binary(int N) 
  8.     {
  9.         int[] binary = new int[(int) Math.pow(2, N)];
  10.         for (int i = 0; i < Math.pow(2, N); i++) 
  11.         {
  12.             int b = 1;
  13.             binary[i] = 0;
  14.             int num = i;
  15.             while (num > 0) 
  16.             {
  17.                 binary[i] += (num % 2) * b;
  18.                 num /= 2;
  19.                 b = b * 10;
  20.             }
  21.         }
  22.         return binary;
  23.     }
  24.  
  25.     public static void main(String args[]) 
  26.     {
  27.         Random random = new Random();
  28.         Scanner sc = new Scanner(System.in);
  29.         System.out.println("Enter the number of elements in the set: ");
  30.         int N = sc.nextInt();
  31.         int[] sequence = new int[N];
  32.         for (int i = 0; i < N; i++)
  33.             sequence[i] = Math.abs(random.nextInt(100));
  34.  
  35.         System.out.println("The elements in the set : ");
  36.         for (int i = 0; i < N; i++)
  37.             System.out.print(sequence[i] + " ");
  38.  
  39.         int[] mask = new int[(int) Math.pow(2, N)];
  40.         mask = binary(N);
  41.  
  42.         System.out.println("\nThe permutations are: ");
  43.         for (int i = 0; i < Math.pow(2, N); i++) 
  44.         {
  45.             System.out.print("{");
  46.             for (int j = 0; j < N; j++) 
  47.             {
  48.                 if (mask[i] % 10 == 1)
  49.                     System.out.print(sequence[j] + " ");
  50.                 mask[i] /= 10;
  51.             }
  52.             System.out.println("}");
  53.         }
  54.         sc.close();
  55.     }
  56. }

Output:

$ javac Binary_Counting_Subsets.java
$ java Binary_Counting_Subsets
 
Enter the number of elements in the set: 
5
The elements in the set : 
78 35 5 10 15 
The permutations are: 
{ }
{ 78 }
{ 35 }
{ 78 35 }
{ 5 }
{ 78 5 }
{ 35 5 }
{ 78 35 5 }
{ 10 }
{ 78 10 }
{ 35 10 }
{ 78 35 10 }
{ 5 10 }
{ 78 5 10 }
{ 35 5 10 }
{ 78 35 5 10 }
{ 15 }
{ 78 15 }
{ 35 15 }
{ 78 35 15 }
{ 5 15 }
{ 78 5 15 }
{ 35 5 15 }
{ 78 35 5 15 }
{ 10 15 }
{ 78 10 15 }
{ 35 10 15 }
{ 78 35 10 15 }
{ 5 10 15 }
{ 78 5 10 15 }
{ 35 5 10 15 }
{ 78 35 5 10 15 }

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.