Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: 21 Nov, 2020

Next Permutation

Moderate
Asked in companies
UberAdobeCurefit

Problem statement

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

Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain an integer ‘N’ representing the length of the permutation.

The second line contains ‘N’ space-separated integers which are the elements of the permutation.
Output Format:
For each test case, print the elements of the lexicographically next greater permutation with a single space-separated. If lexicographically next greater permutation doesn’t exist, print the lexicographically smallest permutation.

Output for every test case will be printed in a separate line.
Note:
You do not need to print anything; It has already been taken care of.
Constraints:
1 <= T <= 50
1 <= N <= 10000
1 <= P[i] <= N

Time limit: 1 sec

Approaches

01 Approach

The basic idea is to generate all the possible permutations of ‘N’ integers and find the lexicographically next greater permutation.

 

Steps are as follows:

  1. Generate all the possible permutations of ‘N’ integers.
  2. Store all the permutations in a 2D array/list.
  3. Sort the list of permutations in lexicographically ascending order.
  4. Find the index of the input permutation in the sorted list. Let say the index is ‘i’.
    • If the given permutation is present at the end of the array/list, no other greater permutation can exist, so print the smallest permutation.
    • Otherwise, return the permutation which is present at the (i+1)th index in the list.

02 Approach

The basic idea of this approach is to develop a greedy algorithm to find the lexicographically next greater permutation.

First of all, note that a permutation of integers which is sorted in descending order, will not have any lexicographically greater permutation. For example, no next permutation will be possible for [4, 3, 2, 1].

 

Now, consider two elements in the given permutation, P[i] and P[j] such that i < j. If P[i] < P[j], we can easily swap the ith and the jth element to generate a permutation which will be necessarily greater than the previous one. But in order to find the smallest among all of them we will have to choose (i, j) such that the ith element will be farthest from the front and if there are multiple options for the jth element, we will choose the smallest one. Why? Since we are considering the lexicographical ordering, a positive change in the element closer to front will generate a larger number than the other one. For example, consider [3, 4, 1, 2], if we choose i = 0, j = 1, then we will get next permutation [4, 3, 1, 2] and if we choose i = 2, j = 3, then we will get a lexicographically smaller permutation [3, 4, 2, 1]. Therefore, we should choose ‘i’ as large as possible.

Now, consider the following steps:

  1. Start iterating from the end of the given permutation and search a pair of indexes (i, i+1) such that P[i] < P[i+1].
  2. If such a pair doesn’t exist, then the permutation is sorted in descending order. And as discussed above, the next permutation will be the lexicographically smallest permutation i.e. the reverse of the given permutation.
  3. Suppose we reached an index ‘i’ such that P[i] < P[i+1]. In this case, we can just swap to (i, i+1)th elements but the resulting permutation may not be the just next greater permutation. Therefore, we will start searching for the smallest element which will be on the right side of the ith element and is greater than the ith element. Let's say the index of that element is ‘j’.
  4. Now, swap P[i] and P[j]. After the swap operation, we will get a lexicographically greater permutation but that again may not be the immediate next one. Since the element at the ith index in the next permutation is greater than the ith element in the given permutation, we can sort all the elements after ith index in ascending order to ensure that the next permutation is just greater one.
  5. Now, notice that the ith index is the first index from the end for which P[i] < P[i+1], i.e. elements in the right side of the ith element must be sorted in descending order. And after doing the swap operation on ith and jth index this order won’t change. Why? Because jth element is the smallest element which is greater than the ith element; i.e. P[j-1] < P[i] < P[j]. Therefore, the resulting sequence (from (i+1)th element to end) is still sorted which means that in order to sort it in ascending order, we can just reverse it.