Lexicographically largest permutation by sequentially inserting Array elements at ends
Last Updated :
28 Feb, 2022
Given an array arr[] of N integers, the task is to find the lexicographically largest permutation by sequentially inserting the array elements to the front or the back of another array.
Examples:
Input: arr[] = {3, 1, 2, 4}
Output: 4 3 1 2
Explanation:
The permutations that can be created by sequentially inserting the array elements to the front or the back of the container are {3, 1, 2, 4}, {1, 3, 2, 4}, {2, 3, 1, 4}, {2, 1, 3, 4}, {4, 1, 3, 2}, {4, 2, 3, 1}, {4, 2, 1, 3}, and {4, 3, 1, 2}. Out of which {4, 3, 1, 2} is the lexicographically largest permutation.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5 4 3 2 1
Approach: The given problem can be solved by using the Greedy Approach using the deque which is based on the observation that if the current array element is at least the first element of the new array, the most optimal choice always is to insert that element in front of the container in order to lexicographically maximize the permutation. Otherwise, insert the element to the end of the array. Follow the steps below to solve the given problem:
- Initialize a deque, say DQ, which stores the current state of the container.
- Initialize a variable, say mx, which stores the maximum till each index representing the 1st element of the deque DQ.
- Traverse the given array arr[] and if the current element arr[i] >= mx, then insert arr[i] to the front of the deque DQ and update the value of mx. Otherwise, insert arr[i] to the back of the deque DQ.
- After completing the above steps, print the elements stored in the deque DQ as the resultant largest permutation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void largestPermutation( int arr[], int N)
{
deque< int > p;
int mx = arr[0];
p.push_back(arr[0]);
for ( int i = 1; i < N; i++) {
if (arr[i] < mx)
p.push_back(arr[i]);
else {
p.push_front(arr[i]);
mx = arr[i];
}
}
for ( auto i : p)
cout << i << " " ;
}
int main()
{
int arr[] = { 3, 1, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
largestPermutation(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void largestPermutation( int arr[], int N)
{
Deque<Integer> p = new LinkedList<Integer>();
int mx = arr[ 0 ];
p.addLast(arr[ 0 ]);
for ( int i = 1 ; i < N; i++) {
if (arr[i] < mx)
p.addLast(arr[i]);
else {
p.addFirst(arr[i]);
mx = arr[i];
}
}
for (Iterator itr = p.iterator(); itr.hasNext();) {
System.out.print(itr.next() + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 2 , 4 };
int N = arr.length;
largestPermutation(arr, N);
}
}
|
Python3
from typing import Deque
def largestPermutation(arr, N):
p = Deque()
mx = arr[ 0 ]
p.append(arr[ 0 ])
for i in range ( 1 , N):
if (arr[i] < mx):
p.append(arr[i])
else :
p.appendleft(arr[i])
mx = arr[i]
for i in p:
print (i, end = " " )
if __name__ = = "__main__" :
arr = [ 3 , 1 , 2 , 4 ]
N = len (arr)
largestPermutation(arr, N)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class GFG {
static void largestPermutation( int []arr, int N) {
List< int > p = new List< int >();
int mx = arr[0];
p.Add(arr[0]);
for ( int i = 1; i < N; i++) {
if (arr[i] < mx)
p.Add(arr[i]);
else {
p.Insert(0,arr[i]);
mx = arr[i];
}
}
foreach ( int itr in p) {
Console.Write(itr + " " );
}
}
public static void Main(String[] args) {
int []arr = { 3, 1, 2, 4 };
int N = arr.Length;
largestPermutation(arr, N);
}
}
|
Javascript
<script>
function largestPermutation(arr, N)
{
let p = [];
let mx = arr[0];
p.push(arr[0]);
for (let i = 1; i < N; i++)
{
if (arr[i] < mx) p.push(arr[i]);
else {
p.unshift(arr[i]);
mx = arr[i];
}
}
for (i of p) document.write(i + " " );
}
let arr = [3, 1, 2, 4];
let N = arr.length;
largestPermutation(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...