C++ Program to Generate All Possible Subsets

This is a C++ program to generate all possible subsets with exactly k elements in each subset.

Problem Description

1. This algorithm print all the possible combination of each length from the given array.
2. The time complexity of this algorithm is O(n*(2^n)).

Problem Solution

1. This algorithm takes the input of ‘n’ data element and prints all possible combination of length ‘k’.
2. For that, it maintains a boolean array of length ‘n’.
3. If the corresponding boolean value is true, then it prints that element.
4. Exit.

Program/Source Code

C++ Program to generate all possible subsets with exactly k elements in each subset
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
 
using namespace std;
 
// A function to print all combination of a given length from the given array.
void GenSubSet(int a[], int reqLen, int start, int currLen, bool check[], int len) 
{
	// Return if the currrLen is more than the required length.
	if(currLen > reqLen)
	return;
	// If currLen is equal to required length then print the sequence.
	else if (currLen == reqLen) 
	{
		cout<<"\t";
		for (int i = 0; i < len; i++) 
		{
			if (check[i] == true) 
			{
				cout<<a[i]<<" ";
			}
		}
		cout<<"\n";
		return;
	}
	// If start equals to len then return since no further element left.
	if (start == len) 
	{
		return;
	}
	// For every index we have two options.
	// First is, we select it, means put true in check[] and increment currLen and start.
	check[start] = true;
	GenSubSet(a, reqLen, start + 1, currLen + 1, check, len);
	// Second is, we don't select it, means put false in check[] and only start incremented.
	check[start] = false;
	GenSubSet(a, reqLen, start + 1, currLen, check, len);
}
 
int main()
{
	int i, n, k;
	bool check[n];
	cout<<"Enter the number of element array have: ";
	cin>>n;
 
	int arr[n];
	cout<<"\n";
 
	// Take the input of the array.
	for(i = 0; i < n; i++)
	{
		cout<<"Enter "<<i+1<<" element: ";
		cin>>arr[i];
		check[i] = false;
	}
 
	cout<<"\nEnter the length of the subsets required: ";
	cin>>k;
 
	cout<<"\nThe combination of length "<<k<<" for the given array set:\n";
	GenSubSet(arr, k, 0, 0, check, n);
 
	return 0;
}
Program Explanation

1. Take the input of an array of ‘n’ data element.
2. Assign the boolean array to false initially.
3. Take the input of k, the length of required subsets and call GenSubSet().
4. In GenSubSet(), if currLen is equal to reqLen then there will be a new sequence generated, print it.
5. There will be two option either proceed with a start as ‘true’ and recursively call GenSubSet() with incremented value of ‘currLen’ and ‘start’.
6. Or proceed with a start as ‘false’ and recursively call GenSubSet() with incremented value of ‘start’.
7. Exit.

advertisement
advertisement
Runtime Test Cases
Case 1:
Enter the number of element array have: 8
 
Enter 1 element: 1
Enter 2 element: 5
Enter 3 element: 3
Enter 4 element: 2
Enter 5 element: 8
Enter 6 element: 4
Enter 7 element: 6
Enter 8 element: 7
 
Enter the length of the subsets required: 3
 
The combination of length 3 for the given array set:
        1 5 3
        1 5 2
        1 5 8
        1 5 4
        1 5 6
        1 5 7
        1 3 2
        1 3 8
        1 3 4
        1 3 6
        1 3 7
        1 2 8
        1 2 4
        1 2 6
        1 2 7
        1 8 4
        1 8 6
        1 8 7
        1 4 6
        1 4 7
        1 6 7
        5 3 2
        5 3 8
        5 3 4
        5 3 6
        5 3 7
        5 2 8
        5 2 4
        5 2 6
        5 2 7
        5 8 4
        5 8 6
        5 8 7
        5 4 6
        5 4 7
        5 6 7
        3 2 8
        3 2 4
        3 2 6
        3 2 7
        3 8 4
        3 8 6
        3 8 7
        3 4 6
        3 4 7
        3 6 7
        2 8 4
        2 8 6
        2 8 7
        2 4 6
        2 4 7
        2 6 7
        8 4 6
        8 4 7
        8 6 7
        4 6 7

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ 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.