Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset

«
»
This is a java program to generate and print all subsets containing exactly k element, where k is provided by user and is <= number of elements in the set. We first find the median of the sequence and then compare with each element such that the difference between the median and number is minimum, and print such k elements. We can achieve this by using Binary Counting method, where we generate first 2^k-1 numbers. The binary number itself represents a subset with 0 as absent element and 1 as present elements. Here is the source code of the Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset. 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 containing exactly K elements in it
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class K_Elements_Subsets 
  6. {
  7.     public static void main(String args[]) 
  8.     {
  9.         Random random = new Random();
  10.         Scanner sc = new Scanner(System.in);
  11.         System.out.println("Enter the number of elements in the set: ");
  12.         int N = sc.nextInt();
  13.         int[] sequence = new int[N];
  14.         for (int i = 0; i < N; i++)
  15.             sequence[i] = Math.abs(random.nextInt(100));
  16.  
  17.         System.out.println("The elements in the set : ");
  18.         for (int i = 0; i < N; i++)
  19.             System.out.print(sequence[i] + " ");
  20.  
  21.         System.out.println("\nEnter the number of elements in the subsets: ");
  22.         int n = sc.nextInt();
  23.  
  24.         int[] binary = new int[(int) Math.pow(2, N)];
  25.         for (int i = 0; i < Math.pow(2, N); i++) 
  26.         {
  27.             int b = 1;
  28.             binary[i] = 0;
  29.             int num = i, count = 0;
  30.             while (num > 0) 
  31.             {
  32.                 if (num % 2 == 1)
  33.                     count++;
  34.                 binary[i] += (num % 2) * b;
  35.                 num /= 2;
  36.                 b = b * 10;
  37.             }
  38.             if (count == n) 
  39.             {
  40.                 System.out.print("{ ");
  41.                 for (int j = 0; j < N; j++) 
  42.                 {
  43.                     if (binary[i] % 10 == 1)
  44.                         System.out.print(sequence[j] + " ");
  45.                     binary[i] /= 10;
  46.                 }
  47.                 System.out.println("}");
  48.             }
  49.         }
  50.         sc.close();
  51.     }
  52. }

Output:

$ javac K_Elements_Subsets.java
$ java K_Elements_Subsets
 
Enter the number of elements in the set: 
6
The elements in the set : 
51 36 33 97 48 22 
Enter the number of elements in the subsets: 
3
{ 51 36 33 }
{ 51 36 97 }
{ 51 33 97 }
{ 36 33 97 }
{ 51 36 48 }
{ 51 33 48 }
{ 36 33 48 }
{ 51 97 48 }
{ 36 97 48 }
{ 33 97 48 }
{ 51 36 22 }
{ 51 33 22 }
{ 36 33 22 }
{ 51 97 22 }
{ 36 97 22 }
{ 33 97 22 }
{ 51 48 22 }
{ 36 48 22 }
{ 33 48 22 }
{ 97 48 22 }
 
Enter the number of elements in the set: 
5
The elements in the set : 
98 74 66 16 76 
Enter the number of elements in the subsets: 
2
{ 98 74 }
{ 98 66 }
{ 74 66 }
{ 98 16 }
{ 74 16 }
{ 66 16 }
{ 98 76 }
{ 74 76 }
{ 66 76 }
{ 16 76 }

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

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

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 & technical discussions at Telegram SanfoundryClasses.