## Algorithm

Problem Name: 31. Next Permutation

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 all the permutations of `arr`: `[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,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.

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]
```

Constraints:

• `1 <= nums.length <= 100`
• `0 <= nums[i] <= 100`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void nextPermutation(int* nums, int numsSize) {
int i, j, k;
for (i = numsSize - 1; i > 0; i --) {
if (nums[i - 1]  <  nums[i]) {
j = i - 1;  // this is the first small number from tail
while (i < numsSize - 1 &&
nums[i + 1] > nums[j]) {  // find the second small number in the tail
i ++;
}
k = nums[i];  // swap it
nums[i] = nums[j];
nums[j] = k;
qsort(&nums[j + 1], numsSize - j - 1, sizeof(int), cmp);
return;
}
}
qsort(nums, numsSize, sizeof(int), cmp);
}
``````
Copy The Code &

Input

cmd
nums = [1,2,3]

Output

cmd
[1,3,2]

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int left = 0, right = -1;
for(int i = nums.size() - 1; i >= 0; i--)
for(int j = i - 1; j >= 0; j--)
if(nums[j]  <  nums[i] && (j > left || right == -1)) left = j, right = i;
if(right == -1) sort(nums.begin(), nums.end());
else{
swap(nums[left], nums[right]);
sort(nums.begin() + left + 1, nums.end());
}
}
};
``````
Copy The Code &

Input

cmd
nums = [3,2,1]

Output

cmd
[1,2,3

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public void nextPermutation(int[] nums) {
int idx = nums.length - 2;
while (idx >= 0 && nums[idx + 1]  < = nums[idx]) {
idx--;
}
if (idx >= 0) {
int endIdx = nums.length - 1;
while (nums[endIdx]  < = nums[idx]) {
endIdx--;
}
swap(nums, idx, endIdx);
}
reverse(nums, idx + 1);
}

private void reverse(int[] nums, int startIdx) {
int endIdx = nums.length - 1;
while (startIdx  <  endIdx) {
swap(nums, startIdx, endIdx);
startIdx++;
endIdx--;
}
}

private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
``````
Copy The Code &

Input

cmd
nums = [1,1,5]

Output

cmd
[1,5,1]

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const nextPermutation = function(nums) {
const n = nums.length
let k
for(let i = n - 2; i >= 0; i--) {
if(nums[i] < nums[i + 1]) {
k = i
break
}
}
if(k == null) {
reverse(nums, 0, n - 1>
} else {
let end
for(let i = n - 1; i >= 0; i--) {
if(nums[i] > nums[k]) {
end = i
break
}
}
swap(nums, k, end)
reverse(nums, k + 1, n - 1)
}

function reverse(arr, start, end) {
while(start < end) {
swap(arr, start, end)
start++
end--
}
}

function swap(arr, i, j> {
;[arr[i], arr[j]] = [arr[j], arr[i]];
}
};
``````
Copy The Code &

Input

cmd
nums = [1,2,3]

Output

cmd
[1,3,2]

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def nextPermutation(self, nums):
perm, l = False, len(nums) - 2
while 0 <= l:
r = len(nums) - 1
while l < r and nums[r] <= nums[l]:
r -= 1
if r <= l:
l -= 1
else:
nums[l], nums[l + 1:], perm = nums[r], sorted(nums[l + 1:r] + [nums[l]] + nums[r + 1:]), True
break
if not perm: nums.sort()
``````
Copy The Code &

Input

cmd
nums = [3,2,1]

Output

cmd
[1,2,3

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

namespace LeetCode
{
public class _031_NextPermutation
{
public void NextPermutation(int[] nums)
{
int i = nums.Length - 1;
while (i > 0 && nums[i - 1] >= nums[i])
i--;

if (i  < = 0)
{
Array.Reverse(nums);
return;
}

int j = nums.Length - 1;
while (j >= 0 && nums[j]  < = nums[i - 1])
j--;

int temp = nums[j];
nums[j] = nums[i - 1];
nums[i - 1] = temp;

Array.Reverse(nums, i, nums.Length - i);
}
}
}
``````
Copy The Code &

Input

cmd
nums = [1,1,5]

Output

cmd
[1,5,1]