Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Next Permutation

XPChallenge Points: 20
levelLevel: Medium

Given a permutation of numbers stored in an array nums,
generate the next permutation sequence that is lexicographically greater than the current one.
If no such permutation exists, rearrange it to become the lowest sequence (ascending order).
The algorithm must modify the array in-place with constant space complexity.

Example 1:

Input: nums = [1,5,8,4,7,6,5,3,1]

Output: [1,5,8,5,1,3,4,6,7]

Example 2:

Input: nums = [1,3,2]

Output: [2,1,3]

Example 3:

Input: nums = [2,3,1]

Output: [3,1,2]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result