For example, suppose we have NextInOrder(10,(1,2,4,7)), then with these two as inputs for the function, I wish to write a python function that returns (1,2,4,8) by finding the next permutation in lexicographic order where elements of the permutation are in the range 1-10
So as another example NextInOrder(10, (5,3,2,10)) would return (5,3,4,1)
You can use a digit counter approach starting from the last position. Increase the last position to a value not in the previous positions. Backtrack to previous position(s) when the next value is out of range.
For example:
output: