Back to DSA
Next Permutation
mediumGiven a sequence of integers representing a permutation, rearrange it to produce the next permutation in lexicographic order. If the current arrangement is the highest possible ordering, wrap around and return the lowest (fully ascending) arrangement. The rearrangement must be done in-place with only constant additional memory.
Examples
Example 1:
Input:
nums = [2,3,1]Output:
[3,1,2]Explanation: The permutation immediately following [2,3,1] in lexicographic sequence is [3,1,2].
Example 2:
Input:
nums = [5,4,3]Output:
[3,4,5]Explanation: This is the highest permutation, so we cycle back to the lowest: [3,4,5].
Hints
1234567