Medium · Arrays

Next Permutation

Given an integer array nums, rearrange it in place into the next lexicographically greater permutation; if nums is already the largest arrangement (non-increasing), rearrange it into ascending order. Nothing is returned: nums itself is the result.

Examples

Example 1

[1,2,3]

Output: [1,3,2]

Example 2

[1,3,5,4,2]

Output: [1,4,2,3,5]

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Arrays problems