Given a list of characters, write a function which would print N choose K possible combinations. Where N is the number of elements in the list and K is the number elements selected.void NChooseK(char *arr, int n, int k)
{
NChooseKHelper(arr, n, k, 0, 0);
}
void NChooseKHelper(char *arr, int n, int k, int index, int level)
{
// Static array will get instantiated only once
static char *res = new char[k];
if (level == k)
{
Display(res, k);
return;
}
for (int i = index; i < n; i++)
{
res[level] = arr[i];
NChooseKHelper(arr, n, k, i + 1, level + 1);
}
}
void Display(char *arr, int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
cout << arr[i];
if (i < size - 1)
cout << ", ";
}
cout << "}" << endl;
} |