#include<iostream>
using namespace std;
void Combination(int numInPair[], int indexInPair, int numArray[],int startOfArray, int endOfArray, int maxCount)
{
if(endOfArray == 0 || indexInPair == maxCount) // Termination Condition
{
for(int i = 0; i< maxCount; i++) // Printing Pair's
{ // Number One By
cout << numInPair[i]<<" "; // One
} //
cout<<"\n"; // Next Line
}
else
{
for(int i = startOfArray; i < endOfArray; i++)
{
numInPair[indexInPair] = numArray[i];
Combination(numInPair,indexInPair+1,numArray,i+1,endOfArray,maxCount);
// indexInPair+1 : proceding to next position in pair
// i+1 : excluding element by incrementing start of array.
// So in next call we iterate for new start to end
}
}
}
void Combination(int * intArray,int arrLength, int pairLength)
{
int * arrPair = new int[pairLength];
Combination(arrPair,0,intArray,0,arrLength,pairLength);
delete[] arrPair;
}
int main()
{
int arrOfNumbers[] = {1, 2, 3, 4, 5};
int length = sizeof(arrOfNumbers)/sizeof(arrOfNumbers[0]);
int sizeOfPair = 3;
Combination(arrOfNumbers,length,sizeOfPair);
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIENvbWJpbmF0aW9uKGludCBudW1JblBhaXJbXSwgaW50IGluZGV4SW5QYWlyLCBpbnQgbnVtQXJyYXlbXSxpbnQgc3RhcnRPZkFycmF5LCBpbnQgZW5kT2ZBcnJheSwgaW50IG1heENvdW50KQp7CiAgICBpZihlbmRPZkFycmF5ID09IDAgfHwgaW5kZXhJblBhaXIgPT0gbWF4Q291bnQpIC8vIFRlcm1pbmF0aW9uIENvbmRpdGlvbgogICAgewogICAgICAgIGZvcihpbnQgaSA9IDA7IGk8IG1heENvdW50OyBpKyspICAvLyAgUHJpbnRpbmcgUGFpcidzCiAgICAgICAgeyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vICBOdW1iZXIgT25lIEJ5IAogICAgICAgICAgICBjb3V0IDw8IG51bUluUGFpcltpXTw8IiAiOyAgICAvLyAgT25lCiAgICAgICAgfSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vCiAgICAgICAgY291dDw8IlxuIjsgICAgICAgICAgICAgICAgICAgICAgIC8vICBOZXh0IExpbmUKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBmb3IoaW50IGkgPSBzdGFydE9mQXJyYXk7IGkgPCBlbmRPZkFycmF5OyBpKyspCiAgICAgICAgewogICAgICAgICAgICBudW1JblBhaXJbaW5kZXhJblBhaXJdID0gbnVtQXJyYXlbaV07ICAKICAgICAgICAgICAgQ29tYmluYXRpb24obnVtSW5QYWlyLGluZGV4SW5QYWlyKzEsbnVtQXJyYXksaSsxLGVuZE9mQXJyYXksbWF4Q291bnQpOwogICAgICAgICAgICAvLyBpbmRleEluUGFpcisxIDogcHJvY2VkaW5nIHRvIG5leHQgcG9zaXRpb24gaW4gcGFpcgogICAgICAgICAgICAvLyBpKzEgICAgICAgICAgIDogZXhjbHVkaW5nIGVsZW1lbnQgYnkgaW5jcmVtZW50aW5nIHN0YXJ0IG9mIGFycmF5LgogICAgICAgICAgICAvLyAgICAgICAgICAgICAgICAgU28gaW4gbmV4dCBjYWxsIHdlIGl0ZXJhdGUgZm9yIG5ldyBzdGFydCB0byBlbmQKICAgICAgICB9CgogICAgfQp9Cgp2b2lkIENvbWJpbmF0aW9uKGludCAqIGludEFycmF5LGludCBhcnJMZW5ndGgsIGludCBwYWlyTGVuZ3RoKQp7CiAgICBpbnQgKiBhcnJQYWlyID0gbmV3IGludFtwYWlyTGVuZ3RoXTsKICAgIENvbWJpbmF0aW9uKGFyclBhaXIsMCxpbnRBcnJheSwwLGFyckxlbmd0aCxwYWlyTGVuZ3RoKTsKICAgIGRlbGV0ZVtdIGFyclBhaXI7Cn0KCmludCBtYWluKCkKewoJaW50IGFyck9mTnVtYmVyc1tdID0gezEsIDIsIDMsIDQsIDV9OwoJaW50IGxlbmd0aCA9IHNpemVvZihhcnJPZk51bWJlcnMpL3NpemVvZihhcnJPZk51bWJlcnNbMF0pOwoJaW50IHNpemVPZlBhaXIgPSAzOwogICAgQ29tYmluYXRpb24oYXJyT2ZOdW1iZXJzLGxlbmd0aCxzaXplT2ZQYWlyKTsKICAgIHJldHVybiAwOwp9