Lexicographically smallest permutation having maximum sum of differences between adjacent elements | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSumPermutation ( vector < int > & arr ) { int N = arr . size ( ) ; sort ( arr . begin ( ) , arr . end ( ) ) ; swap ( arr [ 0 ] , arr [ N - 1 ] ) ; for ( int i : arr ) { cout << i << " ▁ " ; } } int main ( ) { vector < int > arr = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; return 0 ; }