Skip to content
BytePatterns

Next Larger Arrangement

MediumArrays#in-place-reversal#suffix-scan~25m

Problem

A test generator walks through every ordering of a list of numbers in dictionary order. Given a list nums, rearrange it in place into the next larger ordering in dictionary order. If nums is already the largest ordering, wrap around to the smallest one, which is ascending order. Use only O(1) extra space. The list has up to 100 values, and values may repeat.

Examples

Input:  nums = [1, 3, 2]
Output: [2, 1, 3]
Why:    the orderings after [1, 3, 2] in dictionary order start with [2, 1, 3]
Input:  nums = [1, 1, 5]
Output: [1, 5, 1]
Input:  nums = [3, 2, 1]
Output: [1, 2, 3]
Why:    edge case, already the largest ordering, so it wraps to the smallest

Hints

0 / 3

Stuck on the idea rather than the code? In-Place Reversal covers it.