Lexicographically smallest permutation having maximum sum of differences between adjacent elements | Java program for the above approach ; 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 ; Print the required permutation ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void maximumSumPermutation ( int [ ] arr ) { int N = arr . length ; Arrays . sort ( arr ) ; int temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; for ( int i : arr ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; } }