# JavaScript Program for Maximum difference between groups of size two

In this program, we are given an array of integers of even length as we know here we have to make groups of two. We have from that groups using elements of an array, we have to choose two groups among them in order to find the maximum difference between that two groups and we have to return that maximum difference which is going to see in the article below.

## Introduction to Problem

In the given problem we have to find the Maximum difference between groups of size two. It means we have given an array of even length and we have to make groups of size two. As we have to return the maximum difference between the groups, we have to find one highest sum group and one lowest sum group and return the difference value of them.

Example 1

We are given an array of size num,

Input:
Num = 4
Array = {5, 1, 6, 7}
Output: 7


Here groups formed will be the {5,1} and {6,7} i.e. 6 and 13 respectively and the difference between the highest sum and lowest sum is 13 - 6 is 7.

Example 2

Input:
Num =  6
Array = {3, 1, 2, 4, 5, 6}
Output: 8


Here groups formed will be the {1,2}, {3,4} and {5,6} i.e. 3, 7, and 11 respectively, and the difference between the highest sum and lowest sum is 11 - 3 is 8.

## Approach 1: Brute force Approach

In this approach, we create every possible combination of groups and compare each set of combination differences between the group with the highest sum and the group with the lowest sum.

There would be n*(n-1)/2 such groupings in all (nC2).

Time Complexity: O(n^3), since it will require O(n^2) to construct groups and O(n) to check against each group.

## Approach 2: Using the sort Function

In this approach, we first sort the array using the sort function as we know we needed the highest sum and the lowest sum of groups of two, so we added the first and second values of the sorted array the for lowest sum and last and second last value of a sorted array for the highest sum and then return the difference of that sum. Now let's see an example of it.

### Example

JavaScript program for finding the maximum difference between the groups of two using the sort function.

function calMaxDiff( array , num){
//sorting an array using the sort function
array.sort();
let highestVal = array[num - 1] + array[num - 2];
let lowestVal =  array + array ;
return (Math.abs(highestVal - lowestVal));
}
let num = 6;
let array = [3, 1, 2, 4, 5, 6];
console.log("Maximum Difference: " + calMaxDiff( array, num ));


### Time and Space Complexity

The time complexity of the above code is O(NlogN) because here we use the sort function. Here N is a size of an array.

The space complexity of the above code is O(1).

## Approach 3: By Finding a Maximum of Two and a Minimum of Two

In this approach, we have to find the first and second largest values of an array and find the first and second smallest values of an array.

### Example

JavaScript program for finding the maximum difference between the groups of two.

function calMaxDiff( array , num){
let firstMin = Math.min.apply(Math,array);;
let secondMin = Number.MAX_VALUE;
for(let i = 0; i < num ; i ++) {
// If array[i] is not equal to first min
if (array[i] != firstMin)
secondMin = Math.min(array[i],secondMin);
}
let firstMax = Math.max.apply(Math,array);;
let secondMax = Number.MIN_VALUE;
for (let i = 0; i < num ; i ++){
// If array[i] is not equal to first max
if (array[i] != firstMax)
secondMax = Math.max( array[i], secondMax);
}
return Math.abs(firstMax+secondMax-firstMin-secondMin);
}
let num = 6;
let array = [3, 1, 2, 4, 5, 6];
console.log("Maximum Difference: " + calMaxDiff( array, num ));


### Time and Space Complexity

The time complexity of the above code is O(N) because here we only traverse an array for finding the maximum and minimum values. Here N is a size of an array.

The space complexity of the above code is O(1).

## Conclusion

In this article, we have discussed finding the maximum difference between groups of size two. Here we have discussed three approaches to solving the problem. First approach was with O(N^3) time complexity and the following one was with the O(N*log(N)), but the final approach was with time complexity of O(N).

Updated on: 14-Apr-2023

43 Views 