Lexicographically largest permutation by sequentially inserting Array elements at ends | C ++ program for the above approach ; Function to find the lexicographically largest permutation by sequentially inserting the array elements ; Stores the current state of the new array ; Stores ythe current maximum element of array arr [ ] ; Iterate the array elements ; If the current element is smaller than the current maximum , then insert ; If the current element is at least the current maximum ; Update the value of the current maximum ; Print resultant permutation ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }