In the general case, when computing permutations of integers from 1 to k (with repetition):
First set the first permutation as 1 1 1 .... (k times).
Find the rightmost index (say j) so that the element in this index is less than k.
The increment of the element value at index j by one and from the position j + 1 to k reset all elements to 1.
Repeat steps 2 and 3.
Applying this logic, we now get:
1st permutation β 1 1 1.
Then in position 2 (counting the index 0) we have 1 <3, so increase it and reset all the elements after that by 1. The second permutation β 1 1 2.
Then in position 1 (counting the index 0) we have 1 <3, so increase it and reset all the elements after that to 1. Third permutation β 1 2 1
Etc.
source share