Skip to content

Latest commit

 

History

History
56 lines (36 loc) · 1003 Bytes

next_larger_permutation.md

File metadata and controls

56 lines (36 loc) · 1003 Bytes

Next Larger Permutation

Given an array, rearrange its elements into the next numerically greater permutation.

If such permutation is not possible, it must rearranged as the lowest possible order (sorted in an ascending order).

The solution has to be implemented using constant space $$O(1)$$.

Do not use "next permutation" methods from standard libraries.

Example 1

Input: [1, 2, 3]
Output: [1, 3, 2]

Example 2

Input: [1, 3, 2]
Output: [2, 1, 3]

Example 3

Input: [3, 2, 1]
Output: [1, 2, 3]

Explanation

The sequence 3, 2, 1 has no numerically greater permutation, so we return the lowest permutation possible.

Example 4

Input: [1, 1, 5]
Output: [1, 5, 1]

Example 5

Input: [20, 50, 113]
Output: [20, 113, 50]

Solution coming up... :)