I'm trying to figure out an algorithm to have all possible combinations and permutations of size k from an array of size n.
Let's have an example:
Input:
n = 3 => [1, 2, 3]
Output should be:
k = 1 => [[1], [2], [3]]
k = 2 => [[1, 2], [1, 3], [2, 3], [2, 1], [3, 1], [3, 2]]
k = 3 => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1 ,2], [3, 2, 1]]
I started by looking at the QuickPerm Algorithm but it gives all possible permutations for the size of the array:
If we go back to our example, the QuickPerm algorithm gives this output:
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1 ,2], [3, 2, 1]].