# Print all subsets of given size of a set in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array and we have to print all the subset of a given size r that can be formed using the element of the array.

Let’s take an example to understand the topic better −

Input:
array = {3, 5, 6}
r = 2
Output:
3 5
3 6
5 6

In this problem, we will have to find all the combinations of the numbers of the array. And exclude those r bit combinations which are already in the set.

## Example

Live Demo

#include <iostream>
using namespace std;
void printSubset(int arr[], int n, int r, int index, int data[], int i);
int main(){
int arr[] = {3 , 5, 6};
int r = 2;
cout<<"The sets are : ";
int n = sizeof(arr) / sizeof(arr);
int data[r];
printSubset(arr, n, r, 0, data, 0);
return 0;
}
void printSubset(int arr[], int n, int r, int index, int data[], int i){
if (index == r) {
for (int j = 0; j < r; j++)
cout<<data[j]<<" ";
cout<<endl;
return;
}
if (i >= n)
return;
data[index] = arr[i];
printSubset(arr, n, r, index + 1, data, i + 1);
printSubset(arr, n, r, index, data, i + 1);
}

## Output

The sets are −

3 5
3 6
5 6