Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You have been given a permutation of ‘N’ integers. A sequence of ‘N’ integers is called a permutation if it contains all integers from 1 to ‘N’ exactly once. Your task is to rearrange the numbers and generate the lexicographically next greater permutation.

To determine which of the two permutations is lexicographically smaller, we compare their first elements of both permutations. If they are equal — compare the second, and so on. If we have two permutations X and Y, then X is lexicographically smaller if X[i] < Y[i], where ‘i’ is the first index in which the permutations X and Y differ.

For example, [2, 1, 3, 4] is lexicographically smaller than [2, 1, 4, 3].

Detailed explanation

```
1 <= T <= 50
1 <= N <= 10000
1 <= P[i] <= N
Time limit: 1 sec
```

```
2
3
1 2 3
5
2 3 1 4 5
```

```
1 3 2
2 3 1 5 4
```

```
In the first test case, the lexicographically next greater permutation is [1, 3, 2].
In the second test case, the lexicographically next greater permutation is [2, 3, 1, 4, 5].
```

```
2
2
1 2
3
3 1 2
```

```
2 1
3 2 1
```

```
In the first test case, the lexicographically next greater permutation is [2, 1].
In the second test case, the lexicographically next greater permutation is [3, 2, 1].
```