Given an array, we need to find the maximum sum of absolute difference of any permutation of the given array.
Examples:
Input : { 1, 2, 4, 8 }
Output : 18
Explanation : For the given array there are
several sequence possible
like : {2, 1, 4, 8}
{4, 2, 1, 8} and some more.
Now, the absolute difference of an array sequence will be
like for this array sequence {1, 2, 4, 8}, the absolute
difference sum is
= |1-2| + |2-4| + |4-8| + |8-1|
= 14
For the given array, we get the maximum value for
the sequence {1, 8, 2, 4}
= |1-8| + |8-2| + |2-4| + |4-1|
= 18
To solve this problem, we have to think greedily that how can we maximize the difference value of the elements so that we can have a maximum sum. This is possible only if we calculate the difference between some very high values and some very low values like (highest – smallest). This is the idea which we have to use to solve this problem. Let us see the above example, we will have maximum difference possible for sequence {1, 8, 2, 4} because in this sequence we will get some high difference values, ( |1-8| = 7, |8-2| = 6 .. ). Here, by placing 8(highest element) in place of 1 and 2 we get two high difference values. Similarly, for the other values, we will place next highest values in between other, as we have only one left i.e 4 which is placed at last.
Algorithm: To get the maximum sum, we should have a sequence in which small and large elements comes alternate. This is done to get maximum difference.
For the implementation of the above algorithm ->
1. We will sort the array.
2. Calculate the final sequence by taking one smallest element and largest element from the sorted array and make one vector array of this final sequence.
3. Finally, calculate the sum of absolute difference between the elements of the array.
Below is the implementation of above idea :
C++
// CPP implementation of // above algorithm #include <bits/stdc++.h> using namespace std; int MaxSumDifference(int a[], int n) { // final sequence stored in the vector vector<int> finalSequence; // sort the original array // so that we can retrieve // the large elements from // the end of array elements sort(a, a + n); // In this loop first we will insert // one smallest element not entered // till that time in final sequence // and then enter a highest element // (not entered till that time) in // final sequence so that we // have large difference value. This // process is repeated till all array // has completely entered in sequence. // Here, we have loop till n/2 because // we are inserting two elements at a // time in loop. for (int i = 0; i < n / 2; ++i) { finalSequence.push_back(a[i]); finalSequence.push_back(a[n - i - 1]); } // If there are odd elements, push the // middle element at the end. if (n % 2 != 0) finalSequence.push_back(a[n/2]); // variable to store the // maximum sum of absolute // difference int MaximumSum = 0; // In this loop absolute difference // of elements for the final sequence // is calculated. for (int i = 0; i < n - 1; ++i) { MaximumSum = MaximumSum + abs(finalSequence[i] - finalSequence[i + 1]); } // absolute difference of last element // and 1st element MaximumSum = MaximumSum + abs(finalSequence[n - 1] - finalSequence[0]); // return the value return MaximumSum; } // Driver function int main() { int a[] = { 1, 2, 4, 8 }; int n = sizeof(a) / sizeof(a[0]); cout << MaxSumDifference(a, n) << endl; } |
Java
// Java implementation of // above algorithm import java.io.*; import java.util.*; public class GFG { static int MaxSumDifference(Integer []a, int n) { // final sequence stored in the vector List<Integer> finalSequence = new ArrayList<Integer>(); // sort the original array // so that we can retrieve // the large elements from // the end of array elements Arrays.sort(a); // In this loop first we will insert // one smallest element not entered // till that time in final sequence // and then enter a highest element // (not entered till that time) in // final sequence so that we // have large difference value. This // process is repeated till all array // has completely entered in sequence. // Here, we have loop till n/2 because // we are inserting two elements at a // time in loop. for (int i = 0; i < n / 2; ++i) { finalSequence.add(a[i]); finalSequence.add(a[n - i - 1]); } // If there are odd elements, push the // middle element at the end. if (n % 2 != 0) finalSequence.add(a[n/2]); // variable to store the // maximum sum of absolute // difference int MaximumSum = 0; // In this loop absolute difference // of elements for the final sequence // is calculated. for (int i = 0; i < n - 1; ++i) { MaximumSum = MaximumSum + Math.abs(finalSequence.get(i) - finalSequence.get(i + 1)); } // absolute difference of last element // and 1st element MaximumSum = MaximumSum + Math.abs(finalSequence.get(n - 1) - finalSequence.get(0)); // return the value return MaximumSum; } // Driver Code public static void main(String args[]) { Integer []a = { 1, 2, 4, 8 }; int n = a.length; System.out.print(MaxSumDifference(a, n)); } } // This code is contributed by // Manish Shaw (manishshaw1) |
Python3
import numpy as np class GFG: def MaxSumDifference(a,n): # sort the original array # so that we can retrieve # the large elements from # the end of array elements np.sort(a); # In this loop first we will # insert one smallest element # not entered till that time # in final sequence and then # enter a highest element(not # entered till that time) in # final sequence so that we # have large difference value. # This process is repeated till # all array has completely # entered in sequence. Here, # we have loop till n/2 because # we are inserting two elements # at a time in loop. j = 0 finalSequence = [0 for x in range(n)] for i in range(0, int(n / 2)): finalSequence[j] = a[i] finalSequence[j + 1] = a[n - i - 1] j = j + 2 # If there are odd elements, push the # middle element at the end. if (n % 2 != 0): finalSequence[n-1] = a[n/2] # variable to store the # maximum sum of absolute # difference MaximumSum = 0 # In this loop absolute # difference of elements # for the final sequence # is calculated. for i in range(0, n - 1): MaximumSum = (MaximumSum + abs(finalSequence[i] - finalSequence[i + 1])) # absolute difference of last # element and 1st element MaximumSum = (MaximumSum + abs(finalSequence[n - 1] - finalSequence[0])); # return the value print (MaximumSum) # Driver Code a = [ 1, 2, 4, 8 ] n = len(a) GFG.MaxSumDifference(a, n); # This code is contributed # by Prateek Bajaj |
C#
// C# implementation of // above algorithm using System; using System.Collections.Generic; class GFG { static int MaxSumDifference(int []a, int n) { // final sequence stored in the vector List<int> finalSequence = new List<int>(); // sort the original array // so that we can retrieve // the large elements from // the end of array elements Array.Sort(a); // In this loop first we will insert // one smallest element not entered // till that time in final sequence // and then enter a highest element // (not entered till that time) in // final sequence so that we // have large difference value. This // process is repeated till all array // has completely entered in sequence. // Here, we have loop till n/2 because // we are inserting two elements at a // time in loop. for (int i = 0; i < n / 2; ++i) { finalSequence.Add(a[i]); finalSequence.Add(a[n - i - 1]); } // If there are odd elements, push the // middle element at the end. if (n % 2 != 0) finalSequence.Add(a[n/2]); // variable to store the // maximum sum of absolute // difference int MaximumSum = 0; // In this loop absolute difference // of elements for the final sequence // is calculated. for (int i = 0; i < n - 1; ++i) { MaximumSum = MaximumSum + Math.Abs(finalSequence[i] - finalSequence[i + 1]); } // absolute difference of last element // and 1st element MaximumSum = MaximumSum + Math.Abs(finalSequence[n - 1] - finalSequence[0]); // return the value return MaximumSum; } // Driver Code public static void Main() { int []a = { 1, 2, 4, 8 }; int n = a.Length; Console.WriteLine(MaxSumDifference(a, n)); } } // This code is contributed by // Manish Shaw (manishshaw1) |
PHP
<?php // PHP implementation of above algorithm function MaxSumDifference(&$a, $n) { // final sequence stored in the vector $finalSequence = array(); // sort the original array so that we // can retrieve the large elements from // the end of array elements sort($a); // In this loop first we will insert one // smallest element not entered till that // time in final sequence and then enter // a highest element (not entered till // that time) in final sequence so that we // have large difference value. This process // is repeated till all array has completely // entered in sequence. // Here, we have loop till n/2 because // we are inserting two elements at a // time in loop. for ($i = 0; $i < $n / 2; ++$i) { array_push($finalSequence, $a[$i]); array_push($finalSequence, $a[$n - $i - 1]); } // If there are odd elements, push the // middle element at the end. if ($n % 2 != 0) array_push($finalSequence, $a[$n-1]); // variable to store the maximum sum // of absolute difference $MaximumSum = 0; // In this loop absolute difference // of elements for the final sequence // is calculated. for ($i = 0; $i < $n - 1; ++$i) { $MaximumSum = $MaximumSum + abs($finalSequence[$i] - $finalSequence[$i + 1]); } // absolute difference of last element // and 1st element $MaximumSum = $MaximumSum + abs($finalSequence[$n - 1] - $finalSequence[0]); // return the value return $MaximumSum; } // Driver Code $a = array(1, 2, 4, 8 ); $n = sizeof($a); echo MaxSumDifference($a, $n) . "\n"; // This code is contributed by ita_c ?> |
Output :
18
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.
Recommended Posts:
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Maximum absolute difference between sum of two contiguous sub-arrays
- Maximum absolute difference between sum of subarrays of size K
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Maximize minimum distance between repetitions from any permutation of the given Array
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum absolute difference of value and index sums
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Count maximum elements of an array whose absolute difference does not exceed K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

