## 31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
`1,2,3``1,3,2`
`3,2,1``1,2,3`
`1,1,5``1,5,1`

b'
\n\n

## Summary

\n

We need to find the next lexicographic permutation of the given list of numbers than the number formed by the given array.

\n

## Solution

\n
\n

#### Approach #1 Brute Force [Time Limit Exceeded]

\n

Algorithm

\n

In this approach, we find out every possible permutation of list formed by the elements of the given array and find out the permutation which is\njust larger than the given one. But this one will be a very naive approach, since it requires us to find out every possible permutation\n which will take really long time and the implementation is complex.\n Thus, this approach is not acceptable at all. Hence, we move on directly to the correct approach.

\n

Complexity Analysis

\n
\n
• Time complexity : . Total possible permutations is .
• \n
• Space complexity : . Since an array will be used to store the permutations.
• \n
\n
\n

#### Approach #2 Single Pass Approach [Accepted]

\n

Algorithm

\n

First, we observe that for any given sequence that is in descending order, no next larger permutation is possible.\n For example, no next permutation is possible for the following array:\n `[9, 5, 4, 3, 1]`

\n

We need to find the first pair of two successive numbers and , from the right, which satisfy\n . Now, no rearrangements to the right of can create a larger permutation since that subarray consists of numbers in descending order.\n Thus, we need to rearrange the numbers to the right of including itself.

\n

Now, what kind of rearrangement will produce the next larger number? We want to create the permutation just larger than the current one. Therefore, we need to replace the number with the number which is just larger than itself among the numbers lying to its right section, say .

\n \n

We swap the numbers and . We now have the correct number at index . But still the current permutation isn\'t the permutation\n that we are looking for. We need the smallest permutation that can be formed by using the numbers only to the right of . Therefore, we need to place those\n numbers in ascending order to get their smallest permutation.

\n

But, recall that while scanning the numbers from the right, we simply kept decrementing the index\n until we found the pair and where, . Thus, all numbers to the right of were already sorted in descending order.\n Furthermore, swapping and didn\'t change that order.\n Therefore, we simply need to reverse the numbers following to get the next smallest lexicographic permutation.

\n

The following animation will make things clearer:

\n \n

Java

\n
`public class Solution {\n    public void nextPermutation(int[] nums) {\n        int i = nums.length - 2;\n        while (i >= 0 && nums[i + 1] <= nums[i]) {\n            i--;\n        }\n        if (i >= 0) {\n            int j = nums.length - 1;\n            while (j >= 0 && nums[j] <= nums[i]) {\n                j--;\n            }\n            swap(nums, i, j);\n        }\n        reverse(nums, i + 1);\n    }\n\n    private void reverse(int[] nums, int start) {\n        int i = start, j = nums.length - 1;\n        while (i < j) {\n            swap(nums, i, j);\n            i++;\n            j--;\n        }\n    }\n\n    private void swap(int[] nums, int i, int j) {\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n}\n`
\n

Complexity Analysis

\n
\n
• \n

Time complexity : . In worst case, only two scans of the whole array are needed.

\n
• \n
• \n

Space complexity : . No extra space is used. In place replacements are done.

\n
• \n
\n
\n

Analysis written by: @vinod23

\n
'