Print all possible combinations of r elements in a given array of size n - GeeksforGeeks
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}.
// array contains duplicate or not.
Method 1 (Fix Elements and Recur)
Handle Duplicates
http://ideone.com/ywsqBz
Method 2 (Include and Exclude every element)
This is much easier to code.
Read full article from Print all possible combinations of r elements in a given array of size n - GeeksforGeeks
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}.
// array contains duplicate or not.
Method 1 (Fix Elements and Recur)
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 temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring 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);
}
}
Handle Duplicates
http://ideone.com/ywsqBz
- 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, data, 0, n-1, 0, r);
- }
- /* arr[] ---> Input Array
- data[] ---> Temporary array to store current combination
- start & end ---> Staring 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 i=0; i<r; i++)
- printf("%d " ,data[i]);
- 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);
- // Remove duplicates
- while (arr[i] == arr[i+1])
- i++;
- }
- }
Method 2 (Include and Exclude every element)
This is much easier to code.
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 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);
}
Read full article from Print all possible combinations of r elements in a given array of size n - GeeksforGeeks