A permutation of an array of integers is an arrangement of its members into a sequence or linear order.


	For example, for arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].


The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).


	For example, the next permutation of arr = [1,2,3] is [1,3,2].
	Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
	While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.


Given an array of integers nums, find the next permutation of nums.

The replacement must be in place and use only constant extra memory.

&nbsp;
Example 1:

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


Example 2:

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


Example 3:

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


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 100
	0 &lt;= nums[i] &lt;= 100


class Solution:
    def nextPermutation(self, nums) -> None:
        firstDecreasingElement = -1
        toSwapWith = -1
        lastIndex = len(nums) - 1

        # Looking for an element that is less than its follower
        for i in range(lastIndex, 0, -1):
            if nums[i] > nums[i - 1]:
                firstDecreasingElement = i - 1
                break

        # If there is not any then reverse the array to make initial permutation
        if firstDecreasingElement == -1:
            for i in range(0, lastIndex // 2 + 1):
                nums[i], nums[lastIndex - i] = nums[lastIndex - i], nums[i]
            return

        # Looking for an element to swap it with firstDecreasingElement
        for i in range(lastIndex, 0, -1):
            if nums[i] > nums[firstDecreasingElement]:
                toSwapWith = i
                break

        # Swap found elements
        nums[firstDecreasingElement], nums[toSwapWith] = nums[toSwapWith], nums[firstDecreasingElement]

        # Reverse elements from firstDecreasingElement to the end of the array
        left = firstDecreasingElement + 1
        right = lastIndex
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1
            right -= 1
class Solution {
    public void nextPermutation(int[] nums) {
        // FIND peek+1
        int nextOfPeak = -1;
        for (int i = nums.length - 1; i > 0; i--) {
            if (nums[i] > nums[i - 1]) {
                nextOfPeak = i - 1;
                break;
            }
        }

        // Return reverse Array
        if (nextOfPeak == -1) {
            int start = 0;
            int end = nums.length - 1;
            while (start <= end) {
                int temp = nums[start];
                nums[start] = nums[end];
                nums[end] = temp;
                start++;
                end--;
            }
            return;
        }
        // Find element greater than peek
        int reversalPoint = nums.length - 1;
        for (int i = nums.length - 1; i > nextOfPeak; i--) {
            if (nums[i] > nums[nextOfPeak]) {
                reversalPoint = i;
                break;
            }
        }

        // swap nextOfPeak && reversalPoint
        int temp = nums[nextOfPeak];
        nums[nextOfPeak] = nums[reversalPoint];
        nums[reversalPoint] = temp;

        // Reverse array from nextOfPeak+1
        int start = nextOfPeak + 1;
        int end = nums.length - 1;
        while (start <= end) {
            int temp1 = nums[start];
            nums[start] = nums[end];
            nums[end] = temp1;
            start++;
            end--;
        }

    }
}
class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        if(nums.size()==1)
            return;

        int i=nums.size()-2;
        while(i>=0 && nums[i]>=nums[i+1]) i--;
        if(i>=0){
            int j=nums.size()-1;
            while(nums[i] >= nums[j]) j--;
            swap(nums[j], nums[i]);
        }
        sort(nums.begin()+i+1, nums.end());
    }
};
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var nextPermutation = function(nums) {
    const dsc = nums.slice();
    dsc.sort((a, b) => b - a);
    if (dsc.every((n, i) => n === nums[i])) {
        nums.sort((a, b) => a - b);
    } else {
        const len = nums.length;
        let lo = len - 1, hi;
        while (nums[lo] === dsc[dsc.length - 1]) {
            lo--;
            dsc.pop();
        }
        while (lo >= 0) {
            // console.log(lo, nums[lo], nums.slice(lo + 1))
            hi = nums.slice(lo + 1).reverse().findIndex((n) => n > nums[lo]);
            if (hi !== -1) {
                hi = len - 1 - hi;
                break;
            }
            lo--;
        }

        const lval = nums[lo];
        // console.log(lo, lval, hi, nums[hi]);
        nums[lo] = nums[hi];
        nums[hi] = lval;
        const sorted = nums.slice(lo + 1);
        // console.log(nums, sorted)
        sorted.sort((a, b) => a - b);
        for (let i = 0; i < sorted.length; i++)
            nums[lo + 1 + i] = sorted[i];
    }
};
Next Permutation