JavaScript Program for Maximum difference between groups of size two

In this program, we are given an array of integers with even length. We need to form groups of size two using array elements, then find the maximum difference between the group with the highest sum and the group with the lowest sum.

Problem Statement

Given an array of even length, we must partition it into groups of size two. Our goal is to find the maximum possible difference between the sum of any two groups. This means finding the highest sum group and the lowest sum group, then returning their difference.

Examples

Example 1:

Input:
Array = [5, 1, 6, 7]
Output: 7

Groups formed: {5,1} with sum 6, and {6,7} with sum 13. Maximum difference = 13 - 6 = 7.

Example 2:

Input:
Array = [3, 1, 2, 4, 5, 6]  
Output: 8

Groups formed: {1,2} sum=3, {3,4} sum=7, {5,6} sum=11. Maximum difference = 11 - 3 = 8.

Approach 1: Brute Force

Generate all possible group combinations and find the maximum difference. This requires checking all possible ways to partition the array into pairs.

Time Complexity: O(n³) - O(n²) to construct groups and O(n) to find min/max sums.
Space Complexity: O(1)

Approach 2: Using Sort Function

Sort the array first. The minimum sum group will be formed by the two smallest elements, and the maximum sum group by the two largest elements.

function calMaxDiff(array, num) {
    // Sort the array in ascending order
    array.sort((a, b) => a - b);
    
    // Highest sum: two largest elements
    let highestVal = array[num - 1] + array[num - 2];
    
    // Lowest sum: two smallest elements  
    let lowestVal = array[0] + array[1];
    
    return Math.abs(highestVal - lowestVal);
}

let num = 6;
let array = [3, 1, 2, 4, 5, 6];
console.log("Maximum Difference: " + calMaxDiff(array, num));
Maximum Difference: 8

Time Complexity: O(n log n) due to sorting
Space Complexity: O(1)

Approach 3: Finding Two Largest and Two Smallest

Find the first and second largest elements, and the first and second smallest elements directly without sorting.

function calMaxDiff(array, num) {
    let firstMin = Math.min(...array);
    let secondMin = Number.MAX_VALUE;
    
    // Find second minimum
    for (let i = 0; i < num; i++) {
        if (array[i] != firstMin) {
            secondMin = Math.min(array[i], secondMin);
        }
    }
    
    let firstMax = Math.max(...array);
    let secondMax = Number.MIN_VALUE;
    
    // Find second maximum
    for (let i = 0; i < num; i++) {
        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));
Maximum Difference: 8

Time Complexity: O(n) - single pass to find extremes
Space Complexity: O(1)

Comparison of Approaches

Approach Time Complexity Space Complexity Efficiency
Brute Force O(n³) O(1) Poor
Sorting O(n log n) O(1) Good
Min/Max Finding O(n) O(1) Best

Conclusion

We explored three approaches to find the maximum difference between groups of size two. The optimal solution uses direct min/max finding with O(n) time complexity, making it the most efficient approach for large datasets.

Updated on: 2026-03-15T23:19:01+05:30

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements