Given an array of n elements. Consider array as circular array i.e element after an is a1. The task is to find maximum sum of the difference between consecutive elements with rearrangement of array element allowed i.e after rearrangement of element find |a1 – a2| + |a2 – a3| + …… + |an – 1 – an| + |an – a1|.
Examples:
Input : arr[] = { 4, 2, 1, 8 }
Output : 18
Rearrange given array as : { 1, 8, 2, 4 }
Sum of difference between consecutive element
= |1 - 8| + |8 - 2| + |2 - 4| + |4 - 1|
= 7 + 6 + 2 + 3
= 18.
Input : arr[] = { 10, 12, 15 }
Output : 10
The idea is to use Greedy Approach and try to bring elements having greater difference closer.
Consider the sorted permutation of the given array a1, a1, a2,…., an – 1, an such that a1 < a2 < a3…. < an – 1 < an.
Now, to obtain the answer having maximum sum of difference between consecutive element, arrange element in following manner:
a1, an, a2, an-1,…., an/2, a(n/2) + 1
We can observe that the arrangement produces the optimal answer, as all a1, a2, a3,….., a(n/2)-1, an/2 are subtracted twice while a(n/2)+1 , a(n/2)+2, a(n/2)+3,….., an – 1, an are added twice.
Note: a(n/2)+1 This term is considered only for even n because for odd n, it is added once and subtracted once and hence cancels out.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int n)
{
int sum = 0;
sort(arr, arr + n);
for ( int i = 0; i < n/2; i++)
{
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
int main()
{
int arr[] = { 4, 2, 1, 8 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << maxSum(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class MaxSum
{
static int maxSum( int arr[], int n)
{
int sum = 0 ;
Arrays.sort(arr);
for ( int i = 0 ; i < n/ 2 ; i++)
{
sum -= ( 2 * arr[i]);
sum += ( 2 * arr[n - i - 1 ]);
}
return sum;
}
public static void main (String[] args)
{
int arr[] = { 4 , 2 , 1 , 8 };
int n = arr.length;
System.out.println(maxSum(arr, n));
}
}
|
Python3
def maxSum(arr, n):
sum = 0
arr.sort()
for i in range ( 0 , int (n / 2 )) :
sum - = ( 2 * arr[i])
sum + = ( 2 * arr[n - i - 1 ])
return sum
arr = [ 4 , 2 , 1 , 8 ]
n = len (arr)
print (maxSum(arr, n))
|
C#
using System;
class MaxSum {
static int maxSum( int [] arr, int n)
{
int sum = 0;
Array.Sort(arr);
for ( int i = 0; i < n / 2; i++) {
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
public static void Main()
{
int [] arr = { 4, 2, 1, 8 };
int n = arr.Length;
Console.WriteLine(maxSum(arr, n));
}
}
|
Javascript
<script>
function maxSum(arr, n)
{
let sum = 0;
arr.sort();
for (let i = 0; i < n/2; i++)
{
sum -= (2 * arr[i]);
sum += (2 * arr[n - i - 1]);
}
return sum;
}
let arr = [ 4, 2, 1, 8 ];
let n = arr.length;
document.write(maxSum(arr, n));
</script>
|
Time Complexity: O(nlogn).
Auxiliary Space : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Jul, 2022
Like Article
Save Article