
1. It is guaranteed that there will exist a permutation of ‘ARR’ which is lexicographically smaller than ‘ARR’.
2. All integers of ‘ARR’ are not necessarily distinct.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’, representing the size of ‘ARR’
The second line of each test case will contain ‘N’ space-separated integers representing array/list ‘ARR’.
For each test case, print ‘N’ space-separated integers representing the lexicographically largest permutation of ‘ARR’ that is smaller than ‘ARR’ and that can be made by swapping the position of any two integers of ‘ARR’ at different indexes exactly once.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 50
2 <= N <= 10000
1 <= ARR[i] <= 10^9
Where 'ARR[i]' denotes the 'ith'element of ARR.
Time limit: 1 sec
The basic idea is to make an empty integer list/array ‘RESULT’ and try out each permutation that can be obtained by exactly one swap and then compare each of them with ‘ARR’. If the current permutation is lexicographically smaller than ‘ARR’ and lexicographically larger than ‘RESULT’ then makes ‘RESULT’ equal to the current permutation.
The steps are as follows:
Return ‘RESULT’.
We move from the right side of the array/list ‘ARR’ toward the left side until the point where the element is larger than its right neighbour element. Let the index of this element be ‘i’
At this point, ‘ARR[i]’ is one of the elements that should be swapped.
We actually need to swap ‘ARR[i]’ with the largest element on its right side that is less than it. If there are multiple such elements we find the element closest to index ‘i’.
The steps are as follows: