
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all subsets of given size of a set in C++
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
#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[0]); 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
- Related Articles
- Python program to get all subsets of given size of a set
- Python program to get all subsets of a given size of a set
- Find all distinct subsets of a given set in C++
- C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
- Print all possible combinations of r elements in a given array of size n in C++
- How to find all subsets of a set in JavaScript?
- List all the subsets of a set {m , n}
- Count number of subsets of a set with GCD equal to a given number in C++
- Print all sequences of given length in C++
- Sum of XOR of all possible subsets in C++
- C++ Program to Generate All Pairs of Subsets Whose Union Make the Set
- Sum of all subsets of a set formed by first n natural numbers
- Print all interleavings of given two strings in C++
- Print All Distinct Elements of a given integer array in C++
- Program to print all substrings of a given string in C++

Advertisements