Lexicographically largest permutation by sequentially inserting Array elements at ends | 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
from typing import Deque NEW_LINE def largestPermutation ( arr , N ) : NEW_LINE INDENT p = Deque ( ) NEW_LINE mx = arr [ 0 ] NEW_LINE p . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] < mx ) : NEW_LINE INDENT p . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT p . appendleft ( arr [ i ] ) NEW_LINE mx = arr [ i ] NEW_LINE DEDENT DEDENT for i in p : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 1 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE largestPermutation ( arr , N ) NEW_LINE DEDENT