
- 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 possible combinations of r elements in a given array of size n in C++
In this problem, we are given an array of size n and a positive integer r. Our task is to print all possible combinations of the elements of the array of size r.
Let’s take an example to understand the problem −
Input: {5,6,7,8} ; r = 3 Output : {5,6,7}, {5,6,8}, {5,7,8}, {6,7,8}
To solve this problem an approach would be fixing elements and then recuring or looping over others to find all combinations. In this, we have to fix first n-r+1 elements only and loop or recur over the rest.
Example
#include<iostream> using namespace std; void printRElementCombination(int arr[], int combination[], int start, int end, int index, int r){ if (index == r){ cout<<"{ "; for (int j = 0; j < r; j++) cout << combination[j] << " "; cout<<"}\t"; return; } for (int i = start; i <= end && end - i + 1 >= r - index; i++){ combination[index] = arr[i]; printRElementCombination(arr, combination, i+1, end, index+1, r); } } int main(){ int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = 5; int combination[r]; cout<<"The combination is : \n"; printRElementCombination(arr, data, 0, n-1, 0, r); }
Output
The combination is −
{ 1 2 3 } { 1 2 4 } { 1 2 5 } { 1 3 4 } { 1 3 5 } { 1 4 5 } { 2 3 4 } { 2 3 5 } { 2 4 5 } { 3 4 5 }
Other methods to solve the same problem can be by checking the inclusion of current element in the combination and print all combinations of required size. The idea is the same, we will recure over the element and store the combination in combo array. But the fixing of the element is not done.
The below program will make the problem more understandable to you −
Example
#include <iostream> using namespace std; void combinationUtil(int arr[], int n, int r, int index, int combo[], int i){ if (index == r){ cout<<"{"; for (int j = 0; j < r; j++) cout << combo[j] << " "; cout<<"}\t"; return; } if (i >= n) return; combo[index] = arr[i]; combinationUtil(arr, n, r, index + 1, combo, i + 1); combinationUtil(arr, n, r, index, combo, i+1); } int main(){ int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = 5; int combo[r]; cout<<"The combination is : \n"; combinationUtil(arr, n, r, 0, combo, 0); return 0; }
Output
The combination is −
{1 2 3 } {1 2 4 } {1 2 5 } {1 3 4 } {1 3 5 } {1 4 5 } {2 3 4 } {2 3 5 } {2 4 5 } {3 4 5 }
- Related Articles
- Print All Distinct Elements of a given integer array in C++
- Print all combinations of factors in C++
- Print all subsets of given size of a set in C++
- Print all combinations of points that can compose a given number in C++
- C# program to print all distinct elements of a given integer array in C#
- Find minimum possible size of array with given rules for removing elements in C++
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Print all valid words that are possible using Characters of Array in C++\n
- Print all combinations of balanced parentheses in C++
- Finding all possible combinations from an array in JavaScript
- Print all the combinations of a string in lexicographical order in C++
- Print all possible sums of consecutive numbers with sum N in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- How to find all combinations of a vector elements without space in R?
- Print all pairs of anagrams in a given array of strings in C++
