Java Program for Maximum difference between groups of size two
Last Updated :
27 Apr, 2023
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 differences between the group with the 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.
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));
}
}
Output:
11
Time Complexity: O (n * log n)
Space Complexity: O(1) as no extra space has been taken.
Further Optimization :
Instead of sorting, we can find a maximum two and minimum of two in linear time and reduce the time complexity to O(n).
Below is the code for the above approach.
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)
{
int first_min = Arrays.stream(arr).min().getAsInt();
int second_min = Integer.MAX_VALUE;
for(int i = 0; i < n ; i ++)
{
// If arr[i] is not equal to first min
if (arr[i] != first_min)
second_min = Math.min(arr[i],second_min);
}
int first_max = Arrays.stream(arr).max().getAsInt();
int second_max = Integer.MIN_VALUE;
for (int i = 0; i < n ; i ++)
{
// If arr[i] is not equal to first max
if (arr[i] != first_max)
second_max = Math.max(arr[i],second_max);
}
return Math.abs(first_max+second_max-first_min-second_min);
}
// Driver code
public static void main (String[] args) {
int arr[] = { 6, 7, 1, 11 };
int n = arr.length;
System.out.println (CalculateMax(arr, n));
}
}
// This code is contributed by Aman Kumar
Time Complexity: O(N)
Auxiliary Space: O(1)
Please refer complete article on Maximum difference between groups of size two for more details!
Similar Reads
Java Program to Find 2 Elements in the Array such that Difference Between them is Largest An array is the most efficient data structure that is designed to store and access a group of objects. Given an array of integers, our task is to find out two elements from that array such that the difference between them is the maximum. We will be discussing two approaches: By comparing and checkin
4 min read
Java Program for Number of pairs with maximum sum Write a java program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the pai
4 min read
Java Program to Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Ou
5 min read
Java Program to Maximize count of corresponding same elements in given Arrays by Rotation Given two arrays arr1[] and arr2[] of N integers and array arr1[] has distinct elements. The task is to find the maximum count of corresponding same elements in the given arrays by performing cyclic left or right shift on array arr1[]. Examples:  Input: arr1[] = { 6, 7, 3, 9, 5 }, arr2[] = { 7, 3,
3 min read
Java Program For Chocolate Distribution Problem Given an array of n integers where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are m students, the task is to distribute chocolate packets such that: Each student gets one packet.The difference between the number of chocolat
3 min read