# Minimum difference between groups of size two

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

**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[] = {2, 6, 4, 3} Output : 1 Groups formed will be (2, 6) and (4, 3), the difference between highest sum group (2, 6) i.e 8 and lowest sum group (3, 4) i.e 7 is 1. Input : arr[] = {11, 4, 3, 5, 7, 1} Output : 3 Groups formed will be (1, 11), (4, 5) and (3, 7), the difference between highest sum group (1, 11) i.e 12 and lowest sum group (4, 5) i.e 9 is 3.

**Simple Approach:** A simple approach would be to try against all combinations of array elements and check against each set of combination difference between the group with the highest sum and the one with the lowest sum. A total of n*(n-1)/2 such groups would be formed (nC2).

Time Complexity: O(n^3) To generate groups n^2 iterations will be needed and to check against each group n iterations will be needed and hence n^3 iterations will be needed in the worst case.

**Efficient Approach:** Efficient approach would be to use the greedy approach. Sort the whole array and generate groups by selecting one element from the start of the array and one from the end.

## 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 calculate(ll a[], ll n) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `sort(a, a + n); ` ` ` ` ` `// Generating sum groups. ` ` ` `vector<ll> s; ` ` ` `for` `(` `int` `i = 0, j = n - 1; i < j; i++, j--) ` ` ` `s.push_back(a[i] + a[j]); ` ` ` ` ` `ll mini = *min_element(s.begin(), s.end()); ` ` ` `ll maxi = *max_element(s.begin(), s.end()); ` ` ` ` ` `return` `abs` `(maxi - mini); ` `} ` ` ` `int` `main() ` `{ ` ` ` `ll a[] = { 2, 6, 4, 3 }; ` ` ` `int` `n = ` `sizeof` `(a) / (` `sizeof` `(a[0])); ` ` ` `cout << calculate(a, 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.util.Collections; ` `import` `java.util.Vector; ` ` ` ` ` `class` `GFG { ` ` ` `static` `long` `calculate(` `long` `a[], ` `int` `n) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `Arrays.sort(a); ` ` ` `int` `i,j; ` ` ` ` ` `// Generating sum groups. ` ` ` `Vector<Long> s = ` `new` `Vector<>(); ` ` ` `for` `(i = ` `0` `, j = n - ` `1` `; i < j; i++, j--) ` ` ` `s.add((a[i] + a[j])); ` ` ` ` ` `long` `mini = Collections.min(s); ` ` ` `long` `maxi = Collections.max(s); ` ` ` `return` `Math.abs(maxi - mini); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `a[] = { ` `2` `, ` `6` `, ` `4` `, ` `3` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(calculate(a, n)); ` ` ` `} ` `} ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum ` `# difference between groups of ` `# highest and lowest sums. ` `def` `calculate(a, n): ` ` ` ` ` `# Sorting the whole array. ` ` ` `a.sort(); ` ` ` ` ` `# Generating sum groups. ` ` ` `s ` `=` `[]; ` ` ` `i ` `=` `0` `; ` ` ` `j ` `=` `n ` `-` `1` `; ` ` ` `while` `(i < j): ` ` ` `s.append((a[i] ` `+` `a[j])); ` ` ` `i ` `+` `=` `1` `; ` ` ` `j ` `-` `=` `1` `; ` ` ` ` ` `mini ` `=` `min` `(s); ` ` ` `maxi ` `=` `max` `(s); ` ` ` ` ` `return` `abs` `(maxi ` `-` `mini); ` ` ` `# Driver Code ` `a ` `=` `[ ` `2` `, ` `6` `, ` `4` `, ` `3` `]; ` `n ` `=` `len` `(a); ` `print` `(calculate(a, n)); ` ` ` `# This is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// difference between groups of ` `// highest and lowest sums. ` `using` `System; ` `using` `System.Linq; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `long` `calculate(` `long` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `// Sorting the whole array. ` ` ` `Array.Sort(a); ` ` ` `int` `i, j; ` ` ` ` ` `// Generating sum groups. ` ` ` `List<` `long` `> s = ` `new` `List<` `long` `>(); ` ` ` `for` `(i = 0, j = n - 1; i < j; i++, j--) ` ` ` `s.Add((a[i] + a[j])); ` ` ` ` ` `long` `mini = s.Min(); ` ` ` `long` `maxi = s.Max(); ` ` ` `return` `Math.Abs(maxi - mini); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `long` `[]a = { 2, 6, 4, 3 }; ` ` ` `int` `n = a.Length; ` ` ` `Console.WriteLine(calculate(a, n)); ` ` ` `} ` `} ` ` ` `//This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// difference between groups of ` `// highest and lowest sums. ` ` ` `function` `calculate(` `$a` `, ` `$n` `) ` `{ ` ` ` `// Sorting the whole array. ` ` ` `sort(` `$a` `); ` ` ` ` ` `// Generating sum groups. ` ` ` `$s` `= ` `array` `(); ` ` ` `for` `(` `$i` `= 0, ` `$j` `= ` `$n` `- 1; ` ` ` `$i` `< ` `$j` `; ` `$i` `++, ` `$j` `--) ` ` ` `array_push` `(` `$s` `, (` `$a` `[` `$i` `] + ` `$a` `[` `$j` `])); ` ` ` ` ` `$mini` `= min(` `$s` `); ` ` ` `$maxi` `= max(` `$s` `); ` ` ` ` ` `return` `abs` `(` `$maxi` `- ` `$mini` `); ` `} ` ` ` `// Driver Code ` `$a` `= ` `array` `( 2, 6, 4, 3 ); ` `$n` `= sizeof(` `$a` `); ` `echo` `calculate(` `$a` `, ` `$n` `); ` ` ` `// This is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

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

**Asked in:** Inmobi

Reference: https://www.hackerearth.com/problem/algorithm/project-team/

## Recommended Posts:

- Maximum difference between groups of size two
- Divide 1 to n into two groups with minimum sum difference
- Minimum difference between max and min of all K-size subsets
- Reverse an array in groups of given size
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Find array using different XORs of elements in groups of size 4
- k size subsets with maximum difference d between max and min
- Minimum partitions of maximum size 2 and sum limited by given value
- Find maximum (or minimum) sum of a subarray of size k
- Sum of minimum and maximum elements of all subarrays of size k.
- Product of all Subsequences of size K except the minimum and maximum Elements
- Minimum array size after repeated replacement of even sum pair with sum
- Minimum element whose n-th power is greater than product of an array of size n
- Find the minimum sum of distance to A and B from any integer point in a ring of size N

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.