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.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll calculate(ll a[], ll n)
{
sort(a, a + n);
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;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;
class GFG {
static long calculate( long a[], int n)
{
Arrays.sort(a);
int i,j;
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);
}
public static void main(String[] args)
{
long a[] = { 2 , 6 , 4 , 3 };
int n = a.length;
System.out.println(calculate(a, n));
}
}
|
Python3
def calculate(a, n):
a.sort();
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);
a = [ 2 , 6 , 4 , 3 ];
n = len (a);
print (calculate(a, n));
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static long calculate( long []a, int n)
{
Array.Sort(a);
int i, j;
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);
}
public static void Main()
{
long []a = { 2, 6, 4, 3 };
int n = a.Length;
Console.WriteLine(calculate(a, n));
}
}
|
PHP
<?php
function calculate( $a , $n )
{
sort( $a );
$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 );
}
$a = array ( 2, 6, 4, 3 );
$n = sizeof( $a );
echo calculate( $a , $n );
?>
|
Javascript
<script>
function calculate(a, n)
{
a.sort();
let i,j;
let s = [];
for (i = 0, j = n - 1; i < j; i++, j--)
s.push((a[i] + a[j]));
let mini = Math.min(...s);
let maxi = Math.max(...s);
return Math.abs(maxi - mini);
}
let a = [ 2, 6, 4, 3 ];
let n = a.length;
document.write(calculate(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O (n * log n)
- Auxiliary Space: O(n)
Optimizing the above approach:
We can modify the above efficient approach by reducing the auxiliary space from O(n) to O(1). Instead of pushing all the groups in a vector we can directly traverse through the array and keep the track of maximum and minimum element groups.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll calculate(ll a[], ll n)
{
sort(a, a + n);
ll mini = a[0]+a[n-1];
ll maxi = a[0]+a[n-1];
for ( int i = 1, j = n - 2; i < j; i++, j--)
{
if (a[i]+a[j]>maxi)
{
maxi=a[i]+a[j];
}
if (a[i]+a[j]<mini)
{
mini=a[i]+a[j];
}
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int calculate( int [] a, int n)
{
Arrays.sort(a);
int mini = a[ 0 ] + a[n - 1 ];
int maxi = a[ 0 ] + a[n - 1 ];
for ( int i = 1 , j = n - 1 ; i < j; i++, j--) {
if (a[i] + a[j] > maxi) {
maxi = a[i] + a[j];
}
if (a[i] + a[j] < mini) {
mini = a[i] + a[j];
}
}
return Math.abs(maxi - mini);
}
public static void main(String[] args)
{
int [] a = { 2 , 6 , 4 , 3 };
int n = a.length;
System.out.print(calculate(a, n));
}
}
|
Python3
def calculate(a, n):
a.sort()
mini = a[ 0 ] + a[n - 1 ]
maxi = a[ 0 ] + a[n - 1 ]
j = n - 1
for i in range ( 1 , j):
if (a[i] + a[j] > maxi):
maxi = a[i] + a[j]
if (a[i] + a[j] < mini):
mini = a[i] + a[j]
j - = 1
return abs (maxi - mini)
a = [ 2 , 6 , 4 , 3 ]
n = len (a)
print (calculate(a, n))
|
C#
using System;
using System.Linq;
using System.Collections;
public class GFG
{
public static int calculate( int [] a, int n)
{
Array.Sort(a);
var mini = a[0] + a[n - 1];
var maxi = a[0] + a[n - 1];
for ( int i = 1, j = n - 1; i < j; i++, j--)
{
if (a[i] + a[j] > maxi)
{
maxi = a[i] + a[j];
}
if (a[i] + a[j] < mini)
{
mini = a[i] + a[j];
}
}
return Math.Abs(maxi - mini);
}
public static void Main(String[] args)
{
int [] a = {2, 6, 4, 3};
var n = a.Length;
Console.Write(GFG.calculate(a, n));
}
}
|
Javascript
function calculate(a, n)
{
a.sort();
let mini = a[0] + a[n - 1];
let maxi = a[0] + a[n - 1];
for (let i = 1, j = n - 1; i < j; i++, j--) {
if (a[i] + a[j] > maxi) {
maxi = a[i] + a[j];
}
if (a[i] + a[j] < mini) {
mini = a[i] + a[j];
}
}
return Math.abs(maxi - mini);
}
let a = [ 2, 6, 4, 3 ];
let n = a.length;
console.log(calculate(a, n));
|
Complexity Analysis:
- Time Complexity: O (n*log(n))
- Auxiliary Space: O(1)
Asked in: Inmobi