C Program to Generate All Possible Subsets

This is a C Program to generate all possible subsets with exactly K elements in each subset. Generate a permutation and include only k elements in subset.

Here is source code of the C Program to Generate All Possible Subsets with Exactly k Elements in Each Subset. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int main(int argc, char **argv) {
  7.     int i, j;
  8.     time_t seconds;
  9.     time(&seconds);
  10.     srand((unsigned int) seconds);
  11.  
  12.     printf("Enter the number of elements in the set: ");
  13.     int N;
  14.     scanf("%d", &N);
  15.     int sequence[N];
  16.  
  17.     for (i = 0; i < N; i++)
  18.         sequence[i] = rand() % (50 - 1 + 1) + 1;
  19.  
  20.     printf("The elements in the set : ");
  21.     for (i = 0; i < N; i++)
  22.         printf("%d ", sequence[i]);
  23.  
  24.     printf("Enter the number of elements in the subset: ");
  25.     int n;
  26.     scanf("%d", &n);
  27.  
  28.     int binary[(int) pow(2, N)];
  29.     for (i = 0; i < pow(2, N); i++) {
  30.         int b = 1;
  31.         binary[i] = 0;
  32.         int num = i, count = 0;
  33.         while (num > 0) {
  34.             if (num % 2 == 1)
  35.                 count++;
  36.             binary[i] += (num % 2) * b;
  37.             num /= 2;
  38.             b = b * 10;
  39.         }
  40.         if (count == n) {
  41.             printf("{ ");
  42.             for (j = 0; j < N; j++) {
  43.                 if (binary[i] % 10 == 1)
  44.                     printf("%d ", sequence[j]);
  45.                 binary[i] /= 10;
  46.             }
  47.             printf("}\n");
  48.         }
  49.     }
  50.     return 0;
  51. }

Output:

$ gcc KElementsSubset.c
$ ./a.out
 
Enter the number of elements in the set: 5
The elements in the set : 47 44 12 13 23 
Enter the number of elements in the subset: 3
{ 47 44 12 }
{ 47 44 13 }
{ 47 12 13 }
{ 44 12 13 }
{ 47 44 23 }
{ 47 12 23 }
{ 44 12 23 }
{ 47 13 23 }
{ 44 13 23 }
{ 12 13 23 }

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C 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 & discussions at Telegram SanfoundryClasses.