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++
#include<bits/stdc++.h>
using namespace std;
void combinationUtil( int arr[], int data[],
int start, int end,
int index, int r);
void printCombination( int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}
void combinationUtil( int arr[], int data[],
int start, int end,
int index, int r)
{
if (index == r)
{
for ( int j = 0; j < r; j++)
cout << data[j] << " " ;
cout << endl;
return ;
}
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);
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof (arr)/ sizeof (arr[0]);
printCombination(arr, n, r);
}
|
C
#include <stdio.h>
void combinationUtil( int arr[], int data[], int start, int end,
int index, int r);
void printCombination( int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}
void combinationUtil( int arr[], int data[], int start, int end,
int index, int r)
{
if (index == r)
{
for ( int j=0; j<r; j++)
printf ( "%d " , data[j]);
printf ( "\n" );
return ;
}
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);
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof (arr)/ sizeof (arr[0]);
printCombination(arr, n, r);
}
|
Java
import java.io.*;
class Combination {
static void combinationUtil( int arr[], int data[], int start,
int end, int index, int r)
{
if (index == r)
{
for ( int j= 0 ; j<r; j++)
System.out.print(data[j]+ " " );
System.out.println( "" );
return ;
}
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);
}
}
static void printCombination( int arr[], int n, int r)
{
int data[]= new int [r];
combinationUtil(arr, data, 0 , n- 1 , 0 , r);
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int r = 3 ;
int n = arr.length;
printCombination(arr, n, r);
}
}
|
Python3
def printCombination(arr, n, r):
data = [ 0 ] * r;
combinationUtil(arr, data, 0 ,
n - 1 , 0 , r);
def combinationUtil(arr, data, start,
end, index, r):
if (index = = r):
for j in range (r):
print (data[j], end = " " );
print ();
return ;
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 ;
arr = [ 1 , 2 , 3 , 4 , 5 ];
r = 3 ;
n = len (arr);
printCombination(arr, n, r);
|
C#
using System;
class GFG
{
static void combinationUtil( int []arr, int []data,
int start, int end,
int index, int r)
{
if (index == r)
{
for ( int j = 0; j < r; j++)
Console.Write(data[j] + " " );
Console.WriteLine( "" );
return ;
}
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);
}
}
static void printCombination( int []arr,
int n, int r)
{
int []data = new int [r];
combinationUtil(arr, data, 0,
n - 1, 0, r);
}
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.Length;
printCombination(arr, n, r);
}
}
|
Javascript
<script>
function combinationUtil(arr,data,start,end,index,r)
{
if (index == r)
{
for (let j=0; j<r; j++)
{
document.write(data[j]+ " " );
}
document.write( "<br>" )
}
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);
}
}
function printCombination(arr,n,r)
{
let data = new Array(r);
combinationUtil(arr, data, 0, n-1, 0, r);
}
let arr=[1, 2, 3, 4, 5];
let r = 3;
let n = arr.length;
printCombination(arr, n, r);
</script>
|
PHP
<?php
function printCombination( $arr ,
$n , $r )
{
$data = array ();
combinationUtil( $arr , $data , 0,
$n - 1, 0, $r );
}
function combinationUtil( $arr , $data , $start ,
$end , $index , $r )
{
if ( $index == $r )
{
for ( $j = 0; $j < $r ; $j ++)
echo $data [ $j ];
echo "\n" ;
return ;
}
for ( $i = $start ;
$i <= $end &&
$end - $i + 1 >= $r - $index ; $i ++)
{
$data [ $index ] = $arr [ $i ];
combinationUtil( $arr , $data , $i + 1,
$end , $index + 1, $r );
}
}
$arr = array (1, 2, 3, 4, 5);
$r = 3;
$n = sizeof( $arr );
printCombination( $arr , $n , $r );
?>
|
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: 
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++
#include <bits/stdc++.h>
using namespace std;
void combinationUtil( int arr[], int n, int r,
int index, int data[], int i);
void printCombination( int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil( 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];
combinationUtil(arr, n, r, index + 1, data, i + 1);
combinationUtil(arr, n, r, index, data, i+1);
}
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;
}
|
C
#include<stdio.h>
void combinationUtil( int arr[], int n, int r, int index, int data[], int i);
void printCombination( int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil( int arr[], int n, int r, int index, int data[], int i)
{
if (index == r)
{
for ( int j=0; j<r; j++)
printf ( "%d " ,data[j]);
printf ( "\n" );
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
combinationUtil(arr, n, r, index, data, i+1);
}
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
import java.io.*;
class Combination {
static void combinationUtil( int arr[], int n, int r, int index,
int data[], int i)
{
if (index == r)
{
for ( int j= 0 ; j<r; j++)
System.out.print(data[j]+ " " );
System.out.println( "" );
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
combinationUtil(arr, n, r, index+ 1 , data, i+ 1 );
combinationUtil(arr, n, r, index, data, i+ 1 );
}
static void printCombination( int arr[], int n, int r)
{
int data[]= new int [r];
combinationUtil(arr, n, r, 0 , data, 0 );
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int r = 3 ;
int n = arr.length;
printCombination(arr, n, r);
}
}
|
Python 3
def printCombination(arr, n, r):
data = [ 0 ] * r
combinationUtil(arr, n, r, 0 , data, 0 )
def combinationUtil(arr, n, r, index, data, i):
if (index = = r):
for j in range (r):
print (data[j], end = " " )
print ()
return
if (i > = n):
return
data[index] = arr[i]
combinationUtil(arr, n, r, index + 1 ,
data, i + 1 )
combinationUtil(arr, n, r, index,
data, i + 1 )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 ]
r = 3
n = len (arr)
printCombination(arr, n, r)
|
C#
using System;
class GFG
{
static void combinationUtil( int []arr, int n,
int r, int index,
int []data, int i)
{
if (index == r)
{
for ( int j = 0; j < r; j++)
Console.Write(data[j] + " " );
Console.WriteLine( "" );
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
combinationUtil(arr, n, r,
index + 1, data, i + 1);
combinationUtil(arr, n, r, index,
data, i + 1);
}
static void printCombination( int []arr,
int n, int r)
{
int []data = new int [r];
combinationUtil(arr, n, r, 0, data, 0);
}
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.Length;
printCombination(arr, n, r);
}
}
|
Javascript
<script>
function combinationUtil(arr,n,r,index,data,i)
{
if (index == r)
{
for (let j=0; j<r; j++)
{
document.write(data[j]+ " " );
}
document.write( "<br>" );
return ;
}
if (i >= n)
{
return ;
}
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
combinationUtil(arr, n, r, index, data, i+1);
}
function printCombination(arr,n,r)
{
let data= new Array(r);
combinationUtil(arr, n, r, 0, data, 0);
}
let arr=[1, 2, 3, 4, 5];
let r = 3;
let n = arr.length;
printCombination(arr, n, r);
</script>
|
PHP
<?php
function printCombination( $arr , $n , $r )
{
$data = Array();
combinationUtil( $arr , $n , $r ,
0, $data , 0);
}
function combinationUtil( $arr , $n , $r ,
$index , $data , $i )
{
if ( $index == $r )
{
for ( $j = 0; $j < $r ; $j ++)
echo $data [ $j ], " " ;
echo "\n" ;
return ;
}
if ( $i >= $n )
return ;
$data [ $index ] = $arr [ $i ];
combinationUtil( $arr , $n , $r ,
$index + 1,
$data , $i + 1);
combinationUtil( $arr , $n , $r ,
$index , $data , $i + 1);
}
$arr = array (1, 2, 3, 4, 5);
$r = 3;
$n = sizeof( $arr );
printCombination( $arr , $n , $r );
?>
|
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++
#include <stdio.h>
#include <stdlib.h>
void combinationUtil( int arr[], int n, int r, int count, int data[], int i);
int compare ( const void * a, const void * b)
{
return ( *( int *)a - *( int *)b );
}
void printCombination( int arr[], int n, int r)
{
int data[r];
qsort (arr, n, sizeof ( int ), compare);
combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil( int arr[], int n, int r, int index, int data[], int i)
{
if (index == r)
{
for ( int j=0; j<r; j++)
printf ( "%d " ,data[j]);
printf ( "\n" );
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
while (arr[i] == arr[i+1])
i++;
combinationUtil(arr, n, r, index, data, i+1);
}
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
import java.util.Arrays;
public class CombinationGenerator {
public static void main(String[] args) {
int arr[] = { 1 , 2 , 1 , 3 , 1 };
int r = 3 ;
int n = arr.length;
printCombination(arr, n, r);
}
static void printArray( int data[], int r) {
for ( int i = 0 ; i < r; i++)
System.out.print(data[i] + " " );
System.out.println();
}
static void printCombination( int arr[], int n, int r) {
int data[] = new int [r];
Arrays.sort(arr);
combinationUtil(arr, n, r, 0 , data, 0 );
}
static void combinationUtil( int arr[], int n, int r, int index, int data[], int i) {
if (index == r) {
printArray(data, r);
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1 , data, i + 1 );
while (i < n - 1 && arr[i] == arr[i + 1 ])
i++;
combinationUtil(arr, n, r, index, data, i + 1 );
}
}
|
Python3
from itertools import combinations
def print_combination(arr, n, r):
arr.sort()
data = [ 0 ] * r
def combination_util(index, i):
if index = = r:
for j in range (r):
print (data[j], end = " " )
print ()
return
if i > = n:
return
data[index] = arr[i]
combination_util(index + 1 , i + 1 )
while i < n - 1 and arr[i] = = arr[i + 1 ]:
i + = 1
combination_util(index, i + 1 )
combination_util( 0 , 0 )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 3 , 1 ]
r = 3
n = len (arr)
print_combination(arr, n, r)
|
C#
using System;
class Program
{
static void PrintCombination( int [] arr, int n, int r)
{
int [] data = new int [r];
Array.Sort(arr);
CombinationUtil(arr, n, r, 0, data, 0);
}
static void CombinationUtil( int [] arr, int n, int r, int index, int [] data, int i)
{
if (index == r)
{
for ( int j = 0; j < r; j++)
Console.Write(data[j] + " " );
Console.WriteLine();
return ;
}
if (i >= n)
return ;
data[index] = arr[i];
CombinationUtil(arr, n, r, index + 1, data, i + 1);
while (i < n - 1 && arr[i] == arr[i + 1])
i++;
CombinationUtil(arr, n, r, index, data, i + 1);
}
static void Main()
{
int [] arr = { 1, 2, 1, 3, 1 };
int r = 3;
int n = arr.Length;
PrintCombination(arr, n, r);
}
}
|
Javascript
function combinationUtil(arr, n, r, index, data, i) {
if (index === r) {
console.log(data.join( ' ' ));
return ;
}
if (i >= n) return ;
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
while (arr[i] === arr[i + 1]) i++;
combinationUtil(arr, n, r, index, data, i + 1);
}
function compare(a, b) {
return a - b;
}
function printCombination(arr, n, r) {
let data = new Array(r);
arr.sort(compare);
combinationUtil(arr, n, r, 0, data, 0);
}
function main() {
let arr = [1, 2, 1, 3, 1];
let r = 3;
let n = arr.length;
printCombination(arr, n, r);
}
main();
|
Output
1 1 1
1 1 2
1 1 3
1 2 3
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!