The Wayback Machine - https://web.archive.org/web/20210214094539/https://www.geeksforgeeks.org/lexicographically-smallest-permutation-having-maximum-sum-of-differences-between-adjacent-elements/
Skip to content
Related Articles

Related Articles

Lexicographically smallest permutation having maximum sum of differences between adjacent elements
  • Last Updated : 12 Feb, 2021

Given an array arr[] of size N, the task is to find the lexicographically smallest permutation of the given array such that the sum of difference between adjacent elements is maximum.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 5 2 3 4 1
Explanation:
Sum of difference between adjacent elements = (5 – 2) + (2 – 3) + (3 – 4) + (4 – 1) = 4.
{5, 2, 3, 4, 1} is lexicographically the smallest array and 4 is the maximum sum possible.

Input: arr[] = {3, 4, 1}
Output: 4 3 1
Explanation:
Sum of the difference between adjacent elements = (4 – 3) + (3 – 1) = 3
{4, 3, 1} is the lexicographically smallest permutation of array elements possible. Maximum sum of adjacent elements possible is 3.

Naive Approach: The simplest approach is to generate all permutations of the given array and find the sum of each permutation while keeping track of the maximum sum obtained. In the end, print the lexicographically smallest permutation with the maximum sum.
Time Complexity: O(N * N!)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observation:



Let the required permutation of the array be {p1, p2, p3, …, pN – 2, pN – 1, pN}.
Sum of the difference of the adjacent elements, S = (p1-p2) + (p2-p3) +….+ (pN – 2 – pN – 1) + (pN – 1 – pN)
                                                                             = p1 – pn

In order to maximize S, p1 should be the largest element and pN should be the smallest element in the given array arr[]. To build the lexicographically the smallest permutation, arrange the rest of the elements in increasing order. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the lexicographically
// smallest permutation of an array such
// that the sum of the difference between
// adjacent elements is maximum
void maximumSumPermutation(vector<int>& arr)
{
  
    // Stores the size of the array
    int N = arr.size();
  
    // Sort the given array in
    // incresing order
    sort(arr.begin(), arr.end());
  
    // Swap the first and last array elements
    swap(arr[0], arr[N - 1]);
  
    // Print the required permutation
    for (int i : arr) {
  
        cout << i << " ";
    }
}
  
// Driver Code
int main()
{
    vector<int> arr = { 1, 2, 3, 4, 5 };
    maximumSumPermutation(arr);
  
    return 0;
}

chevron_right


Output:

5 2 3 4 1

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :