Lexicographically largest permutation possible by a swap that is smaller than a given array
Last Updated :
06 Jul, 2021
Given an array arr[] consisting of N integers, the task is to find the lexicographically largest permutation of the given array possible by exactly one swap, which is smaller than the given array. If it is possible to obtain such a permutation, then print that permutation. Otherwise, print “-1”.
Examples:
Input: arr[] = {5, 4, 3, 2, 1}
Output: 5 4 3 1 2
Explanation:
Lexicographically, the largest permutation which is smaller than the given array can be formed by swapping 2 and 1.
Hence, the resultant permutation is {5, 4, 3, 1, 2}
Input: arr[] = {1, 2, 3, 4, 5}
Output: -1
Approach: The given problem can be solved by finding the last element which is greater than its next element, and swapping it with the next smaller element in the array. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPermutation(vector< int >& arr)
{
int N = arr.size();
int i = N - 2;
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
if (i == -1) {
cout << "-1" ;
return ;
}
int j = N - 1;
while (j > i && arr[j] >= arr[i])
j--;
while (j > i && arr[j] == arr[j - 1]) {
j--;
}
swap(arr[i], arr[j]);
for ( auto & it : arr) {
cout << it << ' ' ;
}
}
int main()
{
vector< int > arr = { 1, 2, 5, 3, 4, 6 };
findPermutation(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findPermutation( int [] arr)
{
int N = arr.length;
int i = N - 2 ;
while (i >= 0 && arr[i] <= arr[i + 1 ])
i--;
if (i == - 1 )
{
System.out.print( "-1" );
return ;
}
int j = N - 1 ;
while (j > i && arr[j] >= arr[i])
j--;
while (j > i && arr[j] == arr[j - 1 ])
{
j--;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
for ( int it : arr)
{
System.out.print(it + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 5 , 3 , 4 , 6 };
findPermutation(arr);
}
}
|
C#
using System;
class GFG{
static void findPermutation( int [] arr)
{
int N = arr.Length;
int i = N - 2;
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
if (i == -1)
{
Console.Write( "-1" );
return ;
}
int j = N - 1;
while (j > i && arr[j] >= arr[i])
j--;
while (j > i && arr[j] == arr[j - 1])
{
j--;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
foreach ( int it in arr)
{
Console.Write(it + " " );
}
}
public static void Main()
{
int [] arr = { 1, 2, 5, 3, 4, 6 };
findPermutation(arr);
}
}
|
Python3
def findPermutation(arr):
N = len (arr)
i = N - 2
while (i > = 0 and arr[i] < = arr[i + 1 ]):
i - = 1
if (i = = - 1 ) :
print ( "-1" )
return
j = N - 1
while (j > i and arr[j] > = arr[i]):
j - = 1
while (j > i and arr[j] = = arr[j - 1 ]) :
j - = 1
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
for it in arr :
print (it, end = " " )
arr = [ 1 , 2 , 5 , 3 , 4 , 6 ]
findPermutation(arr)
|
Javascript
<script>
function findPermutation(arr)
{
let N = arr.length;
let i = N - 2;
while (i >= 0 && arr[i] <= arr[i + 1])
i--;
if (i == -1)
{
document.write( "-1" );
return ;
}
let j = N - 1;
while (j > i && arr[j] >= arr[i])
j--;
while (j > i && arr[j] == arr[j - 1])
{
j--;
}
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
for (let it in arr)
{
document.write(arr[it] + " " );
}
}
let arr = [ 1, 2, 5, 3, 4, 6 ];
findPermutation(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...