I am trying to generate all k permutations of n values without repetition. For example, the permutations of 2 values from 3: 1, 2, 3
The answer is:
1, 2
1, 3
2, 1
2, 3
3, 1
3, 2
I am currently using next_permutation() from STL that generates all permutations. Also, I found a code to generate combinations of size k. Here is the code:
res = new vector<vector<int>>;
vector<int> a;
for(int i = 0; i < n; i++)
a.push_back(i);
do {
vector<int> b;
for(int i = 0; i < k; i++)
b.push_back(a[i]);
do {
res->push_back(b);
} while(next_permutation(b.begin(), b.begin() + k));
} while(next_combination(a.begin(), a.begin() + k, a.end()));
First, the function finds a new combination and then use next_permutation to generate all permutations of that specific subset.
This code is very time consuming. I wonder if there is better library to produce k permutation in c++ or not?