Lexicographically largest permutation by sequentially inserting Array elements at ends
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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 lexographically 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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the lexicographically// largest permutation by sequentially// inserting the array elementsvoid largestPermutation(int arr[], int N){ // Stores the current state of // the new array deque<int> p; // Stores ythe current maximum // element of array arr[] int mx = arr[0]; p.push_back(arr[0]); // Iterate the array elements for (int i = 1; i < N; i++) { // If the current element is // smaller than the current // maximum, then insert if (arr[i] < mx) p.push_back(arr[i]); // If the current element is // at least the current maximum else { p.push_front(arr[i]); // Update the value of // the current maximum mx = arr[i]; } } // Print resultant permutation for (auto i : p) cout << i << " ";}// Driver Codeint main(){ int arr[] = { 3, 1, 2, 4 }; int N = sizeof(arr) / sizeof(arr[0]); largestPermutation(arr, N); return 0;} |
Python3
# python program for the above approach# Function to find the lexicographically# largest permutation by sequentially# inserting the array elementsfrom typing import Dequedef largestPermutation(arr, N): # Stores the current state of # the new array p = Deque() # Stores ythe current maximum # element of array arr[] mx = arr[0] p.append(arr[0]) # Iterate the array elements for i in range(1, N): # If the current element is # smaller than the current # maximum, then insert if (arr[i] < mx): p.append(arr[i]) # If the current element is # at least the current maximum else: p.appendleft(arr[i]) # Update the value of # the current maximum mx = arr[i] # Print resultant permutation for i in p: print(i, end=" ")# Driver Codeif __name__ == "__main__": arr = [3, 1, 2, 4] N = len(arr) largestPermutation(arr, N)# This code is contributed by rakeshsahni |
Javascript
<script>// Javascript program for the above approach// Function to find the lexicographically// largest permutation by sequentially// inserting the array elementsfunction largestPermutation(arr, N){ // Stores the current state of // the new array let p = []; // Stores ythe current maximum // element of array arr[] let mx = arr[0]; p.push(arr[0]); // Iterate the array elements for (let i = 1; i < N; i++) { // If the current element is // smaller than the current // maximum, then insert if (arr[i] < mx) p.push(arr[i]); // If the current element is // at least the current maximum else { p.unshift(arr[i]); // Update the value of // the current maximum mx = arr[i]; } } // Prlet resultant permutation for (i of p) document.write(i + " ");}// Driver Codelet arr = [3, 1, 2, 4];let N = arr.length;largestPermutation(arr, N);// This code is contributed by gfgking.</script> |
4 3 1 2
Time Complexity: O(N)
Auxiliary Space: O(N)


