0% found this document useful (0 votes)
11 views16 pages

Combination

Uploaded by

saketh.kurra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Combination

Uploaded by

saketh.kurra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

COMBINATION

COMBINATIONS

Introduction

A combination is a subset of elements from a given set

Example: In a card game, we have to deal 5 cards out of the pack consisting of 52
cards. We have no interest in the order in which the 5 cards were selected. Rather, we
only care which cards are present in the hand

Program: Given an array of size n, generate and print all possible combinations of r
elements in array. For example, if input array is {1, 2, 3, 4} and r is 2, then output
should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
COMBINATIONS

Technique-1

We create a temporary array ‘data’ which stores all outputs one by one.

Start with the first index in data, one by one fix elements at this index and recur for
remaining indexes.

Example: arr = [1, 2, 3, 4, 5] r = 3

We first fix 1 at index 0 in data then recur for remaining indexes, then we fix 2 at index
0 and recur.

Then we fix 3 and recur for remaining indexes

When number of elements in data becomes equal to r (size of a combination), we print


data
COMBINATIONS

Technique-1
COMBINATIONS

Technique-1: Handling Duplicates

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 to our code

1) Add code to sort the array before calling combinationUtil() in


printCombination()

2) Add following lines at the end of for loop in combinationUtil()


COMBINATIONS

import java.util.Arrays;

public class Main {


combination[currentIndex] = arr[i];
public static void generateCombinations(int[] arr, int
generateCombinationsUtil(arr,
r) {
combination, currentIndex + 1, i + 1);
int[] combination = new int[r];
}
generateCombinationsUtil(arr, combination, 0,
}
0);
}
public static void main(String[] args) {
int[] arr = {1, 2,2,2};
private static void generateCombinationsUtil(int[]
int r = 2;
arr, int[] combination, int currentIndex, int start) {
Arrays.sort(arr); // Sort the array to
if (currentIndex == combination.length) {
handle duplicates
generateCombinations(arr, r);
System.out.println(Arrays.toString(combination));
}
return;
}
}

for (int i = start; i < arr.length; i++) {


if (i > start && arr[i] == arr[i - 1]) {
continue; // Skip duplicates
COMBINATIONS

Program

comb1.java

Time Complexity: O(2^n)


where 'n' is the length of the array 'arr'.
import java.io.*; static void printCombination(int
public class EthCode { arr[], int n, int r)
static void combinationUtil(int {
arr[], int data[], int start,int end, int combinationUtil(arr, data, 0,
index, int r) n-1, 0, r);
{ }
if (index == r)
{ public static void main
for (int j=0; (String[] args) {
j<r; j++) int arr[] = {1, 2,
3, 4, 5};
System.out.print(data[j]+" "); int r = 3;
int n = arr.length;
System.out.println("");
return; printCombination(arr, n, r);
} }
}
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);
}
}
COMBINATIONS

Technique-2

The idea of technique-2 is similar to the subset-sum problem and is based on


Pascal’s identity

We one by one consider every element of input array, and recur for two
cases:

1) The element is included in current combination


(We put the element in data and increment next available index in data)

2) The element is excluded in current combination (We do not put the


element and do not change index)
COMBINATIONS

Program

comb2.java

Time Complexity: O(n)


COMBINATION-02

import java.io.*; static void printCombination(int arr[],


public class EthCode { int n, int r)
static void combinationUtil(int
{
arr[], int data[], int start,int end, int
index, int r) int data[]=new int[r];
{ combinationUtil(arr, data, 0, n-1, 0, r);
}
if (index == r)
{ public static void main
for (int j=0; j<r; j++) (String[] args) {
System.out.print(data[j]+"
int arr[] = {1, 2, 3,
");
System.out.println(""); 4, 5};
return; int r = 3;
} int n = arr.length;
printCombination(arr,
for (int i=start; i<=end && end-i+1 >= r-index; n, r);
i++) }
{
}
data[index] =
arr[i];
combinationUtil(arr, data, i+1, end, index+1,
r);
while (arr[i] == arr[i+1])
i++;
}}
COMBINATIONS

Program

comb3.java

Time Complexity: O(n)


COMBINATION-03

import java.io.*; static void printCombination(int arr[],


public class EthCode { int n, int r)
static void combinationUtil(int arr[], int n,
{
int r, int index,int data[], int i)
{ int data[]=new int[r];
if (index == r) while (arr[i] == arr[i+1])
{ i++;
for (int j=0; combinationUtil(arr, n, r, 0, data, 0);
j<r; j++) }

System.out.print(data[j]+" ");

System.out.println(""); public static void main


return; (String[] args) {
} int arr[] = {1, 2, 3,
if (i >= n) 4, 5};
return; int r = 3;
data[index] = arr[i]; int n = arr.length;
combinationUtil(arr, n, r, index+1,
printCombination(arr,
data, i+1);
combinationUtil(arr, n, r, index, data, i+1); n, r);
} }
}
/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra

https://
learn.codemithra.com

[email protected] +91 7815 095 +91 9019 921


om 095 340

You might also like