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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Arrays problems