Given an array of **n** elements. Consider array as circular array i.e element after a_{n} is a_{1}. 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 |a_{1} – a_{2}| + |a_{2} – a_{3}| + …… + |a_{n – 1} – a_{n}| + |a_{n} – a_{1}|.

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 a_{1}, a_{1}, a_{2},…., a_{n – 1}, a_{n} such that a_{1} < a_{2} < a_{3}…. < a_{n – 1} < a_{n}.

Now, to obtain the answer having maximum sum of difference between consecutive element, arrange element in following manner:

a_{1}, a_{n}, a_{2}, a_{n-1},…., a_{n/2}, a_{(n/2) + 1}

We can observe that the arrangement produces the optimal answer, as all a_{1}, a_{2}, a_{3},….., a_{(n/2)-1}, a_{n/2} are subtracted twice while a_{(n/2)+1} , a_{(n/2)+2}, a_{(n/2)+3},….., a_{n – 1}, a_{n} 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 substracted once and hence cancels out.

## C++

`// C++ program to maximize the sum of difference ` `// between consecutive elements in circular array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the maximum Sum of difference between ` `// consecutive elements. ` `int` `maxSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Sorting the array. ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Subtracting a1, a2, a3,....., a(n/2)-1, an/2 ` ` ` `// twice and adding a(n/2)+1, a(n/2)+2, a(n/2)+3,. ` ` ` `// ...., an - 1, an twice. ` ` ` `for` `(` `int` `i = 0; i < n/2; i++) ` ` ` `{ ` ` ` `sum -= (2 * arr[i]); ` ` ` `sum += (2 * arr[n - i - 1]); ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 4, 2, 1, 8 }; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << maxSum(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to maximize the sum of difference ` `// between consecutive elements in circular array ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` ` `class` `MaxSum ` `{ ` ` ` `// Return the maximum Sum of difference between ` ` ` `// consecutive elements. ` ` ` `static` `int` `maxSum(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Sorting the array. ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Subtracting a1, a2, a3,....., a(n/2)-1, ` ` ` `// an/2 twice and adding a(n/2)+1, a(n/2)+2, ` ` ` `// a(n/2)+3,....., an - 1, an twice. ` ` ` `for` `(` `int` `i = ` `0` `; i < n/` `2` `; i++) ` ` ` `{ ` ` ` `sum -= (` `2` `* arr[i]); ` ` ` `sum += (` `2` `* arr[n - i - ` `1` `]); ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `4` `, ` `2` `, ` `1` `, ` `8` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(maxSum(arr, n)); ` ` ` `} ` `} ` `/*This code is contributed by Prakriti Gupta*/` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to maximize the sum of difference ` `# between consecutive elements in circular array ` ` ` `# Return the maximum Sum of difference ` `# between consecutive elements ` `def` `maxSum(arr, n): ` ` ` `sum` `=` `0` ` ` ` ` `# Sorting the array ` ` ` `arr.sort() ` ` ` ` ` `# Subtracting a1, a2, a3,....., a(n/2)-1, an/2 ` ` ` `# twice and adding a(n/2)+1, a(n/2)+2, a(n/2)+3,. ` ` ` `# ...., an - 1, an twice. ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `int` `(n ` `/` `2` `)) : ` ` ` `sum` `-` `=` `(` `2` `*` `arr[i]) ` ` ` `sum` `+` `=` `(` `2` `*` `arr[n ` `-` `i ` `-` `1` `]) ` ` ` ` ` `return` `sum` ` ` ` ` `# Driver Program ` `arr ` `=` `[` `4` `, ` `2` `, ` `1` `, ` `8` `] ` `n ` `=` `len` `(arr) ` `print` `(maxSum(arr, n)) ` ` ` `# This code is contributed by Shreyanshi Arun. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to maximize the sum of difference ` `// between consecutive elements in circular array ` `using` `System; ` ` ` `class` `MaxSum { ` ` ` ` ` `// Return the maximum Sum of difference ` ` ` `// between consecutive elements. ` ` ` `static` `int` `maxSum(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Sorting the array. ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Subtracting a1, a2, a3, ....., a(n/2)-1, ` ` ` `// an/2 twice and adding a(n/2)+1, a(n/2)+2, ` ` ` `// a(n/2)+3, ....., an - 1, an twice. ` ` ` `for` `(` `int` `i = 0; i < n / 2; i++) { ` ` ` `sum -= (2 * arr[i]); ` ` ` `sum += (2 * arr[n - i - 1]); ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = { 4, 2, 1, 8 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(maxSum(arr, n)); ` ` ` `} ` `} ` ` ` `//This Code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

Output :

18

**Time Complexity: **O(nlogn).

**Auxiliary Space :** O(1)

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.