// 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;
}
Ly8gUHJvZ3JhbSB0byBwcmludCBhbGwgY29tYmluYXRpb24gb2Ygc2l6ZSByIGluIGFuIGFycmF5IG9mIHNpemUgbgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgp2b2lkIGNvbWJpbmF0aW9uVXRpbChpbnQgYXJyW10sIGludCBuLCBpbnQgciwgaW50IGNvdW50LCBpbnQgZGF0YVtdLCBpbnQgaSk7CiAKLy8gTmVlZGVkIGZvciBxc29ydC4gIFNlZSBodHRwOi8vdy4uLmNvbnRlbnQtYX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N1cnJlbnQgZWxlbWVudCBpbiBhcnJbXSAgICAgKi8Kdm9pZCBjb21iaW5hdGlvblV0aWwoaW50IGFycltdLCBpbnQgbiwgaW50IHIsIGludCBpbmRleCwgaW50IGRhdGFbXSwgaW50IGkpCnsKICAgIC8vIEN1cnJlbnQgY29iaW5hdGlvbiBpcyByZWFkeSwgcHJpbnQgaXQKICAgIGlmIChpbmRleCA9PSByKQogICAgewogICAgICAgIGZvciAoaW50IGo9MDsgajxyOyBqKyspCiAgICAgICAgICAgIHByaW50ZigiJWQgIixkYXRhW2pdKTsKICAgICAgICBwcmludGYoIlxuIik7CiAgICAgICAgcmV0dXJuOwogICAgfQogCiAgICAvLyBXaGVuIG5vIG1vcmUgZWxlbWVudHMgYXJlIHRoZXJlIHRvIGJlIHB1dAogICAgaWYgKGkgPj0gbikKICAgICAgICByZXR1cm47CiAKICAgIC8vIGN1cnJlbnQgaXMgaW5jbHVkZWQsIHB1dCBuZXh0IGF0IG5leHQgbG9jYXRpb24KICAgIGRhdGFbaW5kZXhdID0gYXJyW2ldOwogICAgY29tYmluYXRpb25VdGlsKGFyciwgbiwgciwgaW5kZXgrMSwgZGF0YSwgaSsxKTsKICAgIAogICAgLy8gUmVtb3ZlIGR1cGxpY2F0ZXMKICAgIHdoaWxlIChhcnJbaV0gPT0gYXJyW2krMV0pCiAgICAgICAgaSsrOwogCiAgICAvLyBjdXJyZW50IGlzIGV4Y2x1ZGVkLCByZXBsYWNlIGl0IHdpdGggbmV4dCAoTm90ZSB0aGF0CiAgICAvLyBpKzEgaXMgcGFzc2VkLCBidXQgaW5kZXggaXMgbm90IGNoYW5nZWQpCiAgICBjb21iaW5hdGlvblV0aWwoYXJyLCBuLCByLCBpbmRleCwgZGF0YSwgaSsxKTsKfQogCi8vIERyaXZlciBwcm9ncmFtIHRvIHRlc3QgYWJvdmUgZnVuY3Rpb25zCmludCBtYWluKCkKewogICAgaW50IGFycltdID0gezEsIDIsIDEsIDMsIDF9OwogICAgaW50IHIgPSAzOwogICAgaW50IG4gPSBzaXplb2YoYXJyKS9zaXplb2YoYXJyWzBdKTsKICAgIHByaW50Q29tYmluYXRpb24oYXJyLCBuLCByKTsKICAgIHJldHVybiAwOwp9