Lexicographically largest permutation of array possible by reversing suffix subarrays
Last Updated :
08 Jun, 2021
Given an array arr[] of size N, the task is to find the lexicographically largest permutation array by reversing any suffix subarrays from the array.
Examples:
Input: arr[] = {3, 5, 4, 1, 2}
Output: 3 5 4 2 1
Explanation: Reversing the suffix subarray {1, 2} generates the lexicographically largest permutation of the array elements possible.
Input: arr[] = {3, 5, 1, 2, 1}
Output: 3 5 1 2 1
Explanation:
The given array arr[] is already the lexicographically largest permutation of the array possible.
Naive Approach: The simplest approach is to reverse every possible suffix subarrays from the array and print the permutation of array elements that is lexicographically largest possible.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Greedy Approach. The given problem can be solved based on the following observations:
It can be observed that by choosing the suffix array as subarray in the range [i, N – 1] such that arr[i] < arr[N – 1] and reversing it, the obtained array will be lexicographically the largest array.
Follow the steps below to solve the problem:
- Initialize a variable, say flag as -1, that represents an index is found which has the value less than the last element.
- Traverse the given array arr[] and in each iteration, check if arr[i] < arr[N – 1] or not. Then, store the current index in the variable flag and break the loop.
- After completing the above steps, check if the value of flag is -1 or not. If found to be true, then reverse the suffix subarray i.e., subarray in the range [flag, N – 1].
- After completing the above steps, print the array arr[] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LLA(vector< int > A)
{
int flg = -1;
for ( int i = 0; i < A.size(); i++)
{
if (A[i] < A[A.size() - 1])
{
flg = i;
break ;
}
}
if (flg != -1)
{
for ( int i = flg, j = A.size() - 1;
i <= j; i++, j--)
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
for ( int i = 0; i < A.size(); i++)
cout<<A[i]<< " " ;
}
int main()
{
vector< int > arr= { 3, 5, 4, 1, 2 };
LLA(arr);
}
|
Java
import java.io.*;
class GFG {
public static void LLA( int A[])
{
int flg = - 1 ;
for ( int i = 0 ; i < A.length; i++) {
if (A[i] < A[A.length - 1 ]) {
flg = i;
break ;
}
}
if (flg != - 1 ) {
for ( int i = flg, j = A.length - 1 ;
i <= j; i++, j--) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
for ( int i = 0 ; i < A.length; i++)
System.out.print(A[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 4 , 1 , 2 };
LLA(arr);
}
}
|
Python3
def LLA(A):
flg = - 1 ;
for i in range ( len (A)):
if (A[i] < A[ len (A) - 1 ]):
flg = i;
break ;
if (flg ! = - 1 ):
j = len (A) - 1 ;
for i in range (flg, j + 1 ):
temp = A[i];
A[i] = A[j];
A[j] = temp;
j - = 1 ;
for i in range ( len (A)):
print (A[i], end = " " );
if __name__ = = '__main__' :
arr = [ 3 , 5 , 4 , 1 , 2 ];
LLA(arr);
|
C#
using System;
public class GFG
{
public static void LLA( int []A)
{
int flg = -1;
for ( int i = 0; i < A.Length; i++)
{
if (A[i] < A[A.Length - 1])
{
flg = i;
break ;
}
}
if (flg != -1)
{
for ( int i = flg, j = A.Length - 1;
i <= j; i++, j--)
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
for ( int i = 0; i < A.Length; i++)
Console.Write(A[i] + " " );
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 4, 1, 2 };
LLA(arr);
}
}
|
Javascript
<script>
function LLA(A)
{
var flg = -1;
var i,j;
for (i = 0; i < A.length; i++)
{
if (A[i] < A[A.length - 1])
{
flg = i;
break ;
}
}
if (flg != -1)
{
for (i = flg, j = A.length - 1;
i <= j; i++, j--)
{
var temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
for (i = 0; i < A.length; i++)
document.write(A[i] + " " );
}
var arr = [3, 5, 4, 1, 2];
LLA(arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...