Open In App

Print all combinations | Set-2

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of size n, generate and print all possible combinations of r elements in the array.

Example:

Input: arr=[1,2,3,4], r=2

Output: 1 2
1 3
1 4
2 3
2 4
3 4

Input: arr=[1,2,3,4], r=3

Output: 1 2 3
1 2 4
1 3 4
2 3 4

Method 1 (Fix Elements and Recur) 

The idea is to start from first index (index = 0) , one by one fix elements at this index and recur for remaining indexes. Let the input array be {1, 2, 3, 4, 5} and r be 3. We first fix 1 at index 0, then recur for remaining indexes, then we fix 2 at index 0 and recur. Finally, we fix 3 and recur for remaining indexes. When number of elements becomes equal to r (size of a combination), we print them.

Following diagram shows recursion tree for same input. 


 Following is the implementation of the above approach.  

C++

// C++ program to print all combination // of size r in an array of size n #include<bits/stdc++.h> using namespace std; void combinationUtil(int arr[], int data[], int start, int end, int index, int r); // The main function that prints // all combinations of size r // in arr[] of size n. This function // mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store // all combination one by one int data[r]; // Print all combination using // temporary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ void combinationUtil(int arr[], int data[], int start, int end, int index, int r) { // Current combination is ready // to be printed, print it if (index == r) { for (int j = 0; j < r; j++) cout << data[j] << " "; cout << endl; return; } // replace index with all possible // elements. The condition "end-i+1 >= r-index" // makes sure that including one element // at index will make a combination with // remaining elements at remaining positions for (int i = start; i <= end && end - i + 1 >= r - index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } } // Driver code int main() { int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); } // This code is contributed by rathbhupendra

C

// Program to print all combination of size r in an array of size n #include <stdio.h> void combinationUtil(int arr[], int data[], int start, int end, int index, int r); // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[r]; // Print all combination using temporary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ void combinationUtil(int arr[], int data[], int start, int end, int index, int r) { // Current combination is ready to be printed, print it if (index == r) { for (int j=0; j<r; j++) printf("%d ", data[j]); printf("\n"); return; } // replace index with all possible elements. The condition // "end-i+1 >= r-index" makes sure that including one element // at index will make a combination with remaining elements // at remaining positions for (int i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } } // Driver program to test above functions int main() { int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); }

Java

// Java program to print all combination of size r in an array of size n import java.io.*; class Combination { /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ static void combinationUtil(int arr[], int data[], int start, int end, int index, int r) { // Current combination is ready to be printed, print it if (index == r) { for (int j=0; j<r; j++) System.out.print(data[j]+" "); System.out.println(""); return; } // replace index with all possible elements. The condition // "end-i+1 >= r-index" makes sure that including one element // at index will make a combination with remaining elements // at remaining positions for (int i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } } // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() static void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[]=new int[r]; // Print all combination using temporary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /*Driver function to check for above function*/ public static void main (String[] args) { int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = arr.length; printCombination(arr, n, r); } } /* This code is contributed by Devesh Agrawal */

C#

// C# program to print all // combination of size r // in an array of size n using System; class GFG { /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ static void combinationUtil(int []arr, int []data, int start, int end, int index, int r) { // Current combination is // ready to be printed, // print it if (index == r) { for (int j = 0; j < r; j++) Console.Write(data[j] + " "); Console.WriteLine(""); return; } // replace index with all // possible elements. The // condition "end-i+1 >= // r-index" makes sure that // including one element // at index will make a // combination with remaining // elements at remaining positions for (int i = start; i <= end && end - i + 1 >= r - index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i + 1, end, index + 1, r); } } // The main function that prints // all combinations of size r // in arr[] of size n. This // function mainly uses combinationUtil() static void printCombination(int []arr, int n, int r) { // A temporary array to store // all combination one by one int []data = new int[r]; // Print all combination // using temporary array 'data[]' combinationUtil(arr, data, 0, n - 1, 0, r); } // Driver Code static public void Main () { int []arr = {1, 2, 3, 4, 5}; int r = 3; int n = arr.Length; printCombination(arr, n, r); } } // This code is contributed by m_kit

Javascript

<script> // Javascript program to print all // combination of size r in an array of size n /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ function combinationUtil(arr,data,start,end,index,r) { // Current combination is ready to be printed, print it if (index == r) { for (let j=0; j<r; j++) { document.write(data[j]+" "); } document.write("<br>") } // replace index with all possible elements. The condition // "end-i+1 >= r-index" makes sure that including one element // at index will make a combination with remaining elements // at remaining positions for (let i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } } // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() function printCombination(arr,n,r) { // A temporary array to store all combination one by one let data = new Array(r); // Print all combination using temporary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /*Driver function to check for above function*/ let arr=[1, 2, 3, 4, 5]; let r = 3; let n = arr.length; printCombination(arr, n, r); // This code is contributed by rag2127 </script>

PHP

<?php // Program to print all // combination of size r // in an array of size n // The main function that // prints all combinations // of size r in arr[] of // size n. This function // mainly uses combinationUtil() function printCombination($arr, $n, $r) { // A temporary array to // store all combination // one by one $data = array(); // Print all combination // using temporary array 'data[]' combinationUtil($arr, $data, 0, $n - 1, 0, $r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ function combinationUtil($arr, $data, $start, $end, $index, $r) { // Current combination is ready // to be printed, print it if ($index == $r) { for ($j = 0; $j < $r; $j++) echo $data[$j]; echo "\n"; return; } // replace index with all // possible elements. The // condition "end-i+1 >= // r-index" makes sure that // including one element at // index will make a combination // with remaining elements at // remaining positions for ($i = $start; $i <= $end && $end - $i + 1 >= $r - $index; $i++) { $data[$index] = $arr[$i]; combinationUtil($arr, $data, $i + 1, $end, $index + 1, $r); } } // Driver Code $arr = array(1, 2, 3, 4, 5); $r = 3; $n = sizeof($arr); printCombination($arr, $n, $r); // This code is contributed by ajit ?>

Python3

# Program to print all combination # of size r in an array of size n # The main function that prints # all combinations of size r in # arr[] of size n. This function # mainly uses combinationUtil() def printCombination(arr, n, r): # A temporary array to # store all combination # one by one data = [0]*r; # Print all combination # using temporary array 'data[]' combinationUtil(arr, data, 0, n - 1, 0, r); # arr[] ---> Input Array # data[] ---> Temporary array to # store current combination # start & end ---> Starting and Ending # indexes in arr[] # index ---> Current index in data[] # r ---> Size of a combination # to be printed def combinationUtil(arr, data, start, end, index, r): # Current combination is ready # to be printed, print it if (index == r): for j in range(r): print(data[j], end = " "); print(); return; # replace index with all # possible elements. The # condition "end-i+1 >= # r-index" makes sure that # including one element at # index will make a combination # with remaining elements at # remaining positions i = start; while(i <= end and end - i + 1 >= r - index): data[index] = arr[i]; combinationUtil(arr, data, i + 1, end, index + 1, r); i += 1; # Driver Code arr = [1, 2, 3, 4, 5]; r = 3; n = len(arr); printCombination(arr, n, r); # This code is contributed by mits


Output

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

Time Complexity: [Tex]{n \choose r} [/Tex]
Auxiliary Space: O(r). We use a temporary array data[] of size r to store current combination.

Method 2: Include and Exclude every element

The idea is to explore each element in the array, considering whether to include it or skip it in the current combination. When the combination size reaches r, it’s printed, and this process continues recursively for all elements, ensuring that every valid combination is discovered.

Step-by-step approach:

  • Create a temporary array data[].
  • One by one consider every element of input array, and recur for two cases:
    • The element is included in current combination (We put the element in data[] and increment next available index in data[]) 
    • The element is excluded in current combination (We do not put the element and do not change index)
  • When number of elements in data[] become equal to r (size of a combination), we print it.

Below is the implementation of the above approach:

C++

// C++ Program to print all combination of // size r in an array of size n #include <bits/stdc++.h> using namespace std; void combinationUtil(int arr[], int n, int r, int index, int data[], int i); // The main function that prints all // combinations of size r in arr[] // of size n. This function mainly // uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store // all combination one by one int data[r]; // Print all combination using // temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of current element in arr[] */ void combinationUtil(int arr[], int n, int r, int index, int data[], int i) { // Current combination is ready, print it if (index == r) { for (int j = 0; j < r; j++) cout << data[j] << " "; cout << endl; return; } // When no more elements are there to put in data[] if (i >= n) return; // current is included, put next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index + 1, data, i + 1); // current is excluded, replace it with next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i+1); } // Driver code int main() { int arr[] = {1, 2, 4, 4, 5}; int r = 3; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); return 0; } // This is code is contributed by rathbhupendra

C

// Program to print all combination of size r in an array of size n #include<stdio.h> void combinationUtil(int arr[],int n,int r,int index,int data[],int i); // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[r]; // Print all combination using temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of current element in arr[] */ void combinationUtil(int arr[], int n, int r, int index, int data[], int i) { // Current combination is ready, print it if (index == r) { for (int j=0; j<r; j++) printf("%d ",data[j]); printf("\n"); return; } // When no more elements are there to put in data[] if (i >= n) return; // current is included, put next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index+1, data, i+1); // current is excluded, replace it with next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i+1); } // Driver program to test above functions int main() { int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); return 0; }

Java

// Java program to print all combination of size r in an array of size n import java.io.*; class Combination { /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ static void combinationUtil(int arr[], int n, int r, int index, int data[], int i) { // Current combination is ready to be printed, print it if (index == r) { for (int j=0; j<r; j++) System.out.print(data[j]+" "); System.out.println(""); return; } // When no more elements are there to put in data[] if (i >= n) return; // current is included, put next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index+1, data, i+1); // current is excluded, replace it with next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i+1); } // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() static void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[]=new int[r]; // Print all combination using temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /*Driver function to check for above function*/ public static void main (String[] args) { int arr[] = {1, 2, 3, 4, 5}; int r = 3; int n = arr.length; printCombination(arr, n, r); } } /* This code is contributed by Devesh Agrawal */

C#

// C# program to print all // combination of size r // in an array of size n using System; class GFG { /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ static void combinationUtil(int []arr, int n, int r, int index, int []data, int i) { // Current combination is ready // to be printed, print it if (index == r) { for (int j = 0; j < r; j++) Console.Write(data[j] + " "); Console.WriteLine(""); return; } // When no more elements are // there to put in data[] if (i >= n) return; // current is included, put // next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index + 1, data, i + 1); // current is excluded, replace // it with next (Note that // i+1 is passed, but index // is not changed) combinationUtil(arr, n, r, index, data, i + 1); } // The main function that prints // all combinations of size r // in arr[] of size n. This // function mainly uses combinationUtil() static void printCombination(int []arr, int n, int r) { // A temporary array to store // all combination one by one int []data = new int[r]; // Print all combination // using temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } // Driver Code static public void Main () { int []arr = {1, 2, 3, 4, 5}; int r = 3; int n = arr.Length; printCombination(arr, n, r); } } // This code is contributed by ajit

Javascript

<script> // Javascript program to print all // combination of size r in an array of size n /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Starting and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ function combinationUtil(arr,n,r,index,data,i) { // Current combination is ready // to be printed, print it if (index == r) { for (let j=0; j<r; j++) { document.write(data[j]+" "); } document.write("<br>"); return; } // When no more elements are there // to put in data[] if (i >= n) { return; } // current is included, put // next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index+1, data, i+1); // current is excluded, replace // it with next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i+1); } // The main function that prints // all combinations of size r // in arr[] of size n. This function // mainly uses combinationUtil() function printCombination(arr,n,r) { // A temporary array to store // all combination one by one let data=new Array(r); // Print all combination using // temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /*Driver function to check for above function*/ let arr=[1, 2, 3, 4, 5]; let r = 3; let n = arr.length; printCombination(arr, n, r); // This code is contributed by avanitrachhadiya2155 </script>

PHP

<?php // Program to print all // combination of size r // in an array of size n // The main function that prints // all combinations of size r in // arr[] of size n. This function // mainly uses combinationUtil() function printCombination($arr, $n, $r) { // A temporary array to store // all combination one by one $data = Array(); // Print all combination using // temporary array 'data[]' combinationUtil($arr, $n, $r, 0, $data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of current element in arr[] */ function combinationUtil($arr, $n, $r, $index, $data, $i) { // Current combination // is ready, print it if ($index == $r) { for ($j = 0; $j < $r; $j++) echo $data[$j], " "; echo "\n"; return; } // When no more elements are // there to put in data[] if ($i >= $n) return; // current is included, put // next at next location $data[$index] = $arr[$i]; combinationUtil($arr, $n, $r, $index + 1, $data, $i + 1); // current is excluded, replace // it with next (Note that i+1 // is passed, but index is not changed) combinationUtil($arr, $n, $r, $index, $data, $i + 1); } // Driver Code $arr = array(1, 2, 3, 4, 5); $r = 3; $n = sizeof($arr); printCombination($arr, $n, $r); // This code is contributed by ajit ?>

Python 3

# Program to print all combination # of size r in an array of size n # The main function that prints all # combinations of size r in arr[] of # size n. This function mainly uses # combinationUtil() def printCombination(arr, n, r): # A temporary array to store # all combination one by one data = [0] * r # Print all combination using # temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0) ''' arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of current element in arr[] ''' def combinationUtil(arr, n, r, index, data, i): # Current combination is ready, # print it if (index == r): for j in range(r): print(data[j], end = " ") print() return # When no more elements are # there to put in data[] if (i >= n): return # current is included, put # next at next location data[index] = arr[i] combinationUtil(arr, n, r, index + 1, data, i + 1) # current is excluded, replace it # with next (Note that i+1 is passed, # but index is not changed) combinationUtil(arr, n, r, index, data, i + 1) # Driver Code if __name__ == "__main__": arr = [1, 2, 3, 4, 5] r = 3 n = len(arr) printCombination(arr, n, r) # This code is contributed # by ChitraNayal


Output

1 2 4 1 2 4 1 2 5 1 4 4 1 4 5 1 4 5 2 4 4 2 4 5 2 4 5 4 4 5

Time Complexity: O(2n)
Auxiliary Space : O(r)

Method 3: Handling Duplicates

Note that the above method doesn’t handle duplicates. For example, if input array is {1, 2, 1} and r is 2, then the program prints {1, 2} and {2, 1} as two different combinations. We can avoid duplicates by adding following two additional things to above code.

  • 1) Add code to sort the array before calling combinationUtil() in printCombination() 
  • 2) If the current element is equal to previous element we simply skip that element.

Below is the implementation of above approach:

C++

// Program to print all combination of size r in an array of size n #include <stdio.h> #include <stdlib.h> void combinationUtil(int arr[], int n, int r, int count, int data[], int i); // Needed for qsort. See http ://w...content-available-to-author-only...s.com/reference/cstdlib/qsort/ int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[r]; // Sort array to handle duplicates qsort (arr, n, sizeof(int), compare); // Print all combination using temprary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of current element in arr[] */ void combinationUtil(int arr[], int n, int r, int index, int data[], int i) { // Current cobination is ready, print it if (index == r) { for (int j=0; j<r; j++) printf("%d ",data[j]); printf("\n"); return; } // When no more elements are there to be put if (i >= n) return; // current is included, put next at next location data[index] = arr[i]; combinationUtil(arr, n, r, index+1, data, i+1); // Remove duplicates while (arr[i] == arr[i+1]) i++; // current is excluded, replace it with next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i+1); } // Driver program to test above functions int main() { int arr[] = {1, 2, 1, 3, 1}; int r = 3; int n = sizeof(arr)/sizeof(arr[0]); printCombination(arr, n, r); return 0; }

Java

/*package whatever //do not write package name here */ import java.util.Arrays; public class CombinationGenerator { // The main function that prints all combinations of size r // in arr[] of size n. public static void main(String[] args) { int arr[] = {1, 2, 1, 3, 1}; int r = 3; int n = arr.length; printCombination(arr, n, r); } // A utility function to print an array static void printArray(int data[], int r) { for (int i = 0; i < r; i++) System.out.print(data[i] + " "); System.out.println(); } // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() static void printCombination(int arr[], int n, int r) { // A temporary array to store all combination one by one int data[] = new int[r]; // Sort array to handle duplicates Arrays.sort(arr); // Print all combinations using temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store the current combination i ---> Index of the current element in arr[] */ static void combinationUtil(int arr[], int n, int r, int index, int data[], int i) { // Current combination is ready, print it if (index == r) { printArray(data, r); return; } // When no more elements are there to be put if (i >= n) return; // Current element is included, put the next element at the next location data[index] = arr[i]; combinationUtil(arr, n, r, index + 1, data, i + 1); // Remove duplicates while (i < n - 1 && arr[i] == arr[i + 1]) i++; // Current element is excluded, replace it with the next (Note that // i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i + 1); } } // This code is contributed by akshitaguprzj3

C#

using System; class Program { // The main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() static void PrintCombination(int[] arr, int n, int r) { // A temporary array to store all combinations one by one int[] data = new int[r]; // Sort array to handle duplicates Array.Sort(arr); // Print all combinations using temporary array 'data[]' CombinationUtil(arr, n, r, 0, data, 0); } /* arr[] ---> Input Array n ---> Size of input array r ---> Size of a combination to be printed index ---> Current index in data[] data[] ---> Temporary array to store current combination i ---> index of the current element in arr[] */ static void CombinationUtil(int[] arr, int n, int r, int index, int[] data, int i) { // Current combination is ready, print it if (index == r) { for (int j = 0; j < r; j++) Console.Write(data[j] + " "); Console.WriteLine(); return; } // When no more elements are there to be put if (i >= n) return; // Current is included, put next at the next location data[index] = arr[i]; CombinationUtil(arr, n, r, index + 1, data, i + 1); // Remove duplicates while (i < n - 1 && arr[i] == arr[i + 1]) i++; // Current is excluded, replace it with next (Note that // i+1 is passed, but index is not changed) CombinationUtil(arr, n, r, index, data, i + 1); } // Driver program to test above functions static void Main() { int[] arr = { 1, 2, 1, 3, 1 }; int r = 3; int n = arr.Length; PrintCombination(arr, n, r); } }

Javascript

// Program to print all combinations of size r in an array of size n // Function to perform combination utility function combinationUtil(arr, n, r, index, data, i) { // Current combination is ready, print it if (index === r) { console.log(data.join(' ')); return; } // When no more elements are there to be put if (i >= n) return; // Current element is included, put the next at the next location data[index] = arr[i]; combinationUtil(arr, n, r, index + 1, data, i + 1); // Remove duplicates while (arr[i] === arr[i + 1]) i++; // Current element is excluded, replace it with the next // (Note that i+1 is passed, but index is not changed) combinationUtil(arr, n, r, index, data, i + 1); } // Function to sort array needed for handling duplicates function compare(a, b) { return a - b; } // Main function that prints all combinations of size r // in arr[] of size n. This function mainly uses combinationUtil() function printCombination(arr, n, r) { // A temporary array to store all combinations one by one let data = new Array(r); // Sort array to handle duplicates arr.sort(compare); // Print all combinations using temporary array 'data[]' combinationUtil(arr, n, r, 0, data, 0); } // Driver program to test the above functions function main() { let arr = [1, 2, 1, 3, 1]; let r = 3; let n = arr.length; printCombination(arr, n, r); } // Execute the main function main(); // This code is contributed by akshitaguprzj3

Python3

from itertools import combinations # Function to print all combinations of size r in an array of size n def print_combination(arr, n, r): # Sort the array to handle duplicates arr.sort() # A temporary list to store combinations one by one data = [0] * r # Function to print all combinations using the temporary list 'data' def combination_util(index, i): # Current combination is ready, print it if index == r: for j in range(r): print(data[j], end=" ") print() return # When there are no more elements to be added if i >= n: return # Include the current element and move to the next location data[index] = arr[i] combination_util(index + 1, i + 1) # Skip duplicates while i < n - 1 and arr[i] == arr[i + 1]: i += 1 # Exclude the current element and move to the next (Note that i+1 is passed, but index is not changed) combination_util(index, i + 1) # Start the combination generation process combination_util(0, 0) # Driver program to test the function if __name__ == "__main__": arr = [1, 2, 1, 3, 1] r = 3 n = len(arr) print_combination(arr, n, r)


Output

1 1 1 1 1 2 1 1 3 1 2 3


 



Last Updated : 28 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads