Java Program to Generate All Pairs of Subsets Whose Union Make the Set

«
»
This is a java program to generate and print all the pair of subsets whose union makes the original set.

Here is the source code of the Java Program to Generate All Pairs of Subsets Whose Union Make the 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 pair of subsets whose union results the original set
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Pair_Subset_Union_Set 
  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
  43.                 .println("\nThe pair of permutations whose union is original set are: ");
  44.         for (int i = 0; i < (Math.pow(2, N) / 2); i++) 
  45.         {
  46.             System.out.print("{ ");
  47.             for (int j = 0; j < N; j++) 
  48.             {
  49.                 if (mask[i] % 10 == 1)
  50.                     System.out.print(sequence[j] + " ");
  51.                 mask[i] /= 10;
  52.             }
  53.             System.out.print("} and ");
  54.  
  55.             System.out.print("{ ");
  56.             for (int j = 0; j < N; j++) 
  57.             {
  58.                 if (mask[(int) Math.pow(2, N) - 1 - i] % 10 == 1)
  59.                     System.out.print(sequence[j] + " ");
  60.                 mask[(int) Math.pow(2, N) - 1 - i] /= 10;
  61.             }
  62.             System.out.println("}");
  63.         }
  64.         sc.close();
  65.     }
  66. }

Output:

advertisement
$ javac Pair_Subset_Union_Set.java
$ java Pair_Subset_Union_Set
 
Enter the number of elements in the set: 
5
The elements in the set : 
3 47 97 79 8 
The pair of permutations whose union is original set are: 
{ } and { 3 47 97 79 8 }
{ 3 } and { 47 97 79 8 }
{ 47 } and { 3 97 79 8 }
{ 3 47 } and { 97 79 8 }
{ 97 } and { 3 47 79 8 }
{ 3 97 } and { 47 79 8 }
{ 47 97 } and { 3 79 8 }
{ 3 47 97 } and { 79 8 }
{ 79 } and { 3 47 97 8 }
{ 3 79 } and { 47 97 8 }
{ 47 79 } and { 3 97 8 }
{ 3 47 79 } and { 97 8 }
{ 97 79 } and { 3 47 8 }
{ 3 97 79 } and { 47 8 }
{ 47 97 79 } and { 3 8 }
{ 3 47 97 79 } and { 8 }
 
Enter the number of elements in the set: 
3
The elements in the set : 
37 76 87 
The pair of permutations whose union is original set are: 
{ } and { 37 76 87 }
{ 37 } and { 76 87 }
{ 76 } and { 37 87 }
{ 37 76 } and { 87 }

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter