Go through this code -:
using namespace std;
int K;
void printallpossible(char arr[], int N, string res, int k)
{
if (k == K)
{
cout << res << endl;
return;
}
for (int i = 0; i < N; i++)
{
res += arr[i];
printallpossible(arr, N, res, k + 1);
res.erase(res.end() - 1);
}
}
int main()
{
char arr[] = {'a', 'b'};
int N = sizeof(arr) / sizeof(char);
K = 3;
printallpossible(arr, N, "", 0);
}