Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.

**Note:** An element can be a part of one group only and it has to be a part of at least 1 group.

**Examples:**

Input : arr[] = {1, 4, 9, 6} Output : 10 Groups formed will be (1, 4) and (6, 9), the difference between highest sum group (6, 9) i.e 15 and lowest sum group (1, 4) i.e 5 is 10. Input : arr[] = {6, 7, 1, 11} Output : 11 Groups formed will be (1, 6) and (7, 11), the difference between highest sum group (7, 11) i.e 18 and lowest sum group (1, 6) i.e 7 is 11.

**Simple Approach:** We can solve this problem by making all possible combinations and checking each set of combination difference between the group with highest sum and with the lowest sum. A total of n*(n-1)/2 such groups would be formed (nC2).

Time Complexity: O(n^3), because it will take O(n^2) to generate groups and to check against each group n iterations will be needed thus overall it takes O(n^3) time.

**Efficient Approach:** We can use the greedy approach. Sort the whole array and our result is sum of last two elements minus sum of first two elements.

## C++

`// CPP program to find minimum difference ` `// between groups of highest and lowest ` `// sums. ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `ll CalculateMax(ll arr[], ` `int` `n) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `sort(arr, arr + n); ` ` ` ` ` `int` `min_sum = arr[0] + arr[1]; ` ` ` `int` `max_sum = arr[n-1] + arr[n-2]; ` ` ` ` ` `return` `abs` `(max_sum - min_sum); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll arr[] = { 6, 7, 1, 11 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << CalculateMax(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum difference ` `// between groups of highest and lowest ` `// sums. ` `import` `java.util.Arrays; ` `import` `java.io.*; ` ` ` `class` `GFG { ` `static` `int` `CalculateMax(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `Arrays.sort(arr); ` ` ` ` ` `int` `min_sum = arr[` `0` `] + arr[` `1` `]; ` ` ` `int` `max_sum = arr[n-` `1` `] + arr[n-` `2` `]; ` ` ` ` ` `return` `(Math.abs(max_sum - min_sum)); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `arr[] = { ` `6` `, ` `7` `, ` `1` `, ` `11` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println (CalculateMax(arr, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find minimum difference ` `# between groups of highest and lowest ` `def` `CalculateMax(arr, n): ` ` ` ` ` `# Sorting the whole array. ` ` ` `arr.sort() ` ` ` `min_sum ` `=` `arr[` `0` `] ` `+` `arr[` `1` `] ` ` ` `max_sum ` `=` `arr[n ` `-` `1` `] ` `+` `arr[n ` `-` `2` `] ` ` ` `return` `abs` `(max_sum ` `-` `min_sum) ` ` ` `# Driver code ` `arr ` `=` `[` `6` `, ` `7` `, ` `1` `, ` `11` `] ` `n ` `=` `len` `(arr) ` `print` `(CalculateMax(arr, n)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum difference ` `// between groups of highest and lowest ` `// sums. ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `static` `int` `CalculateMax(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `Array.Sort(arr); ` ` ` ` ` `int` `min_sum = arr[0] + arr[1]; ` ` ` `int` `max_sum = arr[n-1] + arr[n-2]; ` ` ` ` ` `return` `(Math.Abs(max_sum - min_sum)); ` `} ` ` ` `// Driver code ` ` ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `[]arr = { 6, 7, 1, 11 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(CalculateMax(arr, n)); ` ` ` `} ` `//This code is contributed by Sachin. ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// difference between groups of ` `// highest and lowest sums. ` `function` `CalculateMax(` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `sort(` `$arr` `); ` ` ` ` ` `$min_sum` `= ` `$arr` `[0] + ` ` ` `$arr` `[1]; ` ` ` `$max_sum` `= ` `$arr` `[` `$n` `- 1] + ` ` ` `$arr` `[` `$n` `- 2]; ` ` ` ` ` `return` `abs` `(` `$max_sum` `- ` ` ` `$min_sum` `); ` `} ` ` ` `// Driver code ` `$arr` `= ` `array` `(6, 7, 1, 11 ); ` `$n` `= sizeof(` `$arr` `); ` `echo` `CalculateMax(` `$arr` `, ` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity:** O (n * log n)

**Further Optimization : **

Instead of sorting, we can find maximum two and minimum two in linear time and reduce time complexity to O(n).

## Recommended Posts:

- Minimum difference between groups of size two
- Partition the array into two odd length groups with minimized absolute difference between their median
- Find array using different XORs of elements in groups of size 4
- Maximize number of groups formed with size not smaller than its largest element
- Maximum size of square such that all submatrices of that size have sum less than K
- Divide N segments into two non-empty groups such that given condition is satisfied
- Min difference between maximum and minimum element in all Y size subarrays
- Maximum absolute difference between sum of subarrays of size K
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Maximum difference between two subsets of m elements
- Program to find the maximum difference between the index of any two different numbers
- Maximum distance between two elements whose absolute difference is K
- Maximum absolute difference between any two level sum in a N-ary Tree
- Subsequence with maximum pairwise absolute difference and minimum size
- Maximum of minimum difference of all pairs from subsequences of given size
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Split N^2 numbers into N groups of equal sum
- Maximize the happiness of the groups on the Trip
- Sort groups of numbers and characters separately in a String

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.