Lexicographically smallest permutation having maximum sum of differences between adjacent elements | Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Prvar the required permutation ; Driver Code
function maximumSumPermutation ( arr ) { var N = arr . length ; arr . sort ( ( a , b ) => a - b ) ; var temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; document . write ( arr ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; maximumSumPermutation ( arr ) ;