FrontendX
Next Permutation Generator
medium
Description
AI Assistance
Solution
Test Cases
Test Result
Submissions
Canvas
Next Permutation
Generator
Rearrange numbers into the next lexicographically greater permutation or smallest if not possible.
Examples
Example 1
Input:
[1,2,3]
Output:
[1,3,2]
Example 2
Input:
[1,2,3]
Output:
[1,3,2]
Hints
Hint 1
Find the longest non-increasing suffix.
Hint 2
Pivot is the element just before this suffix.
Hint 3
Swap pivot with next larger element, then reverse the suffix.
Next Permutation Generator
Rearrange numbers into the next lexicographically greater permutation or smallest if not possible.