Given an array of integers arr[] of the even size n. Form the groups of two elements each from the given array arr[], such that the difference between the group with highest sum and the group with lowest sum is minimum. Your task is to find the minimum possible difference.
Note: An element can be a part of only one group, and all the elements need to be part of some group.
Example:
Input: arr[] = [ 2, 6, 4, 3 ]
Output: 1
Explanation: The two groups can be [2, 6] and [4, 7] with different between their sum of elements = 2 + 6 - 4 + 3 = 1, which is the minimum possible.Input: arr[] = [ 11, 4, 3, 5, 7, 1 ]
Output: 3
Explanation: The one possible combination of groups are [11, 1], [3, 7] and [4, 5], with the sum of their elements 12, 10, and 9 respectively.
[Naive Approach] - Using Sorting - O(n^3) Time
A simple approach would be to try all combinations of array elements and check against each set of combination and 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).
[Expected Approach] - Using Sorting - O(n * log n) Time and O(n) Space
It can be observed that the difference between sum of groups of elements will be smallest if the largest element is paired with the smallest one, and similarly the second largest element is paired up with the second smallest element.
The idea is to sort the given array arr[], group the first element with the last element, and the second element with the second last element and so on. Store the sum of these groups in an auxiliary array. Thereafter, find the highest and lowest sum, the difference between two is the required answer.
Below is given the implementation:
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum difference
// between sum of elements of groups
int minDiff(vector<int> &arr) {
int n = arr.size();
// sort the array
sort(arr.begin(), arr.end());
// to store the sum of groups of elements
vector<int> sum;
for(int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
sum.push_back(s);
}
// to store the highest and lowest sum
int high = INT_MIN, low = INT_MAX;
for(int i = 0; i < sum.size(); i++) {
high = max(high, sum[i]);
low = min(low, sum[i]);
}
return high - low;
}
int main() {
vector<int> arr = {2, 6, 4, 3};
cout << minDiff(arr);
return 0;
}
// Function to find the minimum difference
// between sum of elements of groups
import java.util.*;
class GfG {
// Function to find the minimum difference
// between sum of elements of groups
static int minDiff(int[] arr) {
int n = arr.length;
// sort the array
Arrays.sort(arr);
// to store the sum of groups of elements
ArrayList<Integer> sum = new ArrayList<>();
for (int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
sum.add(s);
}
// to store the highest and lowest sum
int high = Integer.MIN_VALUE, low = Integer.MAX_VALUE;
for (int i = 0; i < sum.size(); i++) {
high = Math.max(high, sum.get(i));
low = Math.min(low, sum.get(i));
}
return high - low;
}
public static void main(String[] args) {
int[] arr = {2, 6, 4, 3};
System.out.println(minDiff(arr));
}
}
# Function to find the minimum difference
# between sum of elements of groups.
def minDiff(arr):
n = len(arr)
# sort the array
arr.sort()
# to store the sum of groups of elements
sumList = []
for i in range(n // 2):
# group the elements and find sum
s = arr[i] + arr[n - i - 1]
sumList.append(s)
# to store the highest and lowest sum
high = -float('inf')
low = float('inf')
for i in range(len(sumList)):
high = max(high, sumList[i])
low = min(low, sumList[i])
return high - low
if __name__ == "__main__":
arr = [2, 6, 4, 3]
print(minDiff(arr))
// Function to find the minimum difference
// between sum of elements of groups
using System;
using System.Collections.Generic;
class GfG {
// Function to find the minimum difference
// between sum of elements of groups
static int minDiff(List<int> arr) {
int n = arr.Count;
// sort the array
arr.Sort();
// to store the sum of groups of elements
List<int> sum = new List<int>();
for (int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
sum.Add(s);
}
// to store the highest and lowest sum
int high = int.MinValue, low = int.MaxValue;
for (int i = 0; i < sum.Count; i++) {
high = Math.Max(high, sum[i]);
low = Math.Min(low, sum[i]);
}
return high - low;
}
static void Main() {
List<int> arr = new List<int> {2, 6, 4, 3};
Console.WriteLine(minDiff(arr));
}
}
// Function to find the minimum difference
// between sum of elements of groups.
function minDiff(arr) {
let n = arr.length;
// sort the array
arr.sort((a, b) => a - b);
// to store the sum of groups of elements
let sum = [];
for (let i = 0; i < Math.floor(n / 2); i++) {
// group the elements and find sum
let s = arr[i] + arr[n - i - 1];
sum.push(s);
}
// to store the highest and lowest sum
let high = -Infinity, low = Infinity;
for (let i = 0; i < sum.length; i++) {
high = Math.max(high, sum[i]);
low = Math.min(low, sum[i]);
}
return high - low;
}
let arr = [2, 6, 4, 3];
console.log(minDiff(arr));
Output
1
[Optimized Approach] - Using Sorting - O(n * log n) Time and O(1) Space
In the above approach, instead of storing the sum of groups of elements, the idea is to directly compute the highest and lowest sum as we only need these two values. And after grouping all the elements, return the difference of the highest and lowest sum.
Below is given the implementation:
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum difference
// between sum of elements of groups
int minDiff(vector<int> &arr) {
int n = arr.size();
// sort the array
sort(arr.begin(), arr.end());
// to store the highest and lowest sum
int high = INT_MIN, low = INT_MAX;
for(int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
// update the highest and lowest sum
high = max(high, s);
low = min(low, s);
}
return high - low;
}
int main() {
vector<int> arr = {2, 6, 4, 3};
cout << minDiff(arr);
return 0;
}
// Function to find the minimum difference
// between sum of elements of groups
import java.util.*;
class GfG {
// Function to find the minimum difference
// between sum of elements of groups
static int minDiff(int[] arr) {
int n = arr.length;
// sort the array
Arrays.sort(arr);
// to store the highest and lowest sum
int high = Integer.MIN_VALUE, low = Integer.MAX_VALUE;
for (int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
// update the highest and lowest sum
high = Math.max(high, s);
low = Math.min(low, s);
}
return high - low;
}
public static void main(String[] args) {
int[] arr = {2, 6, 4, 3};
System.out.println(minDiff(arr));
}
}
# Function to find the minimum difference
# between sum of elements of groups.
def minDiff(arr):
n = len(arr)
# sort the array
arr.sort()
# to store the highest and lowest sum
high = -float('inf')
low = float('inf')
for i in range(n // 2):
# group the elements and find sum
s = arr[i] + arr[n - i - 1]
# update the highest and lowest sum
high = max(high, s)
low = min(low, s)
return high - low
if __name__ == "__main__":
arr = [2, 6, 4, 3]
print(minDiff(arr))
// Function to find the minimum difference
// between sum of elements of groups
using System;
using System.Collections.Generic;
class GfG {
// Function to find the minimum difference
// between sum of elements of groups
static int minDiff(List<int> arr) {
int n = arr.Count;
// sort the array
arr.Sort();
// to store the highest and lowest sum
int high = int.MinValue, low = int.MaxValue;
for (int i = 0; i < n / 2; i++) {
// group the elements and find sum
int s = arr[i] + arr[n - i - 1];
// update the highest and lowest sum
high = Math.Max(high, s);
low = Math.Min(low, s);
}
return high - low;
}
static void Main() {
List<int> arr = new List<int> { 2, 6, 4, 3 };
Console.WriteLine(minDiff(arr));
}
}
// Function to find the minimum difference
// between sum of elements of groups.
function minDiff(arr) {
let n = arr.length;
// sort the array
arr.sort((a, b) => a - b);
// to store the highest and lowest sum
let high = -Infinity, low = Infinity;
for (let i = 0; i < Math.floor(n / 2); i++) {
// group the elements and find sum
let s = arr[i] + arr[n - i - 1];
// update the highest and lowest sum
high = Math.max(high, s);
low = Math.min(low, s);
}
return high - low;
}
let arr = [2, 6, 4, 3];
console.log(minDiff(arr));
Output
1