Next Permutation
Last updated
Last updated
The Breakpoint is used to βfind the position of the element where the digits dip down after being ascending for a while from right to left
In this example, digits from right to left keep increasing from 4 -> 5 -> 6. Then it dips down at 3.
If there is no such tipping point, the number is entirely made of descending digits and there is no next permutation. So the result is the reverse of that number. Example .
Find the digit that is just larger than the tipping point.
Since the digits on the right are in ascending order, we can just traverse it from right to left. As soon as we find a value that is larger that the breakpoint we can be sure that this is the digit that is just larger than the breakpoint.
Any digit on its left is too large and on its right is too small.
Swap the breakpoint and the just larger element
Make the digits after the breakpoint ascending.
Time Complexity:
Space Complexity: β