JavaScript Program for Number of Pairs with Maximum Sum



JavaScript program for number of pairs with maximum sum is a common coding problem that involves finding the number of pairs in an array that have the maximum sum. This problem can be solved using various approaches which we will be discussing with code examples and their stepwise explanation.

In this article we are having an array of integers. Our task is to write a JavaScript program for number of pairs with maximum sum.

Example 1

Input: 
array = [1, 2, 3, 4, 5]

Pair sum of (1, 2) = 3, Pair sum of (1, 3) = 4
Pair sum of (1, 4) = 5, Pair sum of (1, 5) = 6
Pair sum of (2, 3) = 5, Pair sum of (2, 4) = 6
Pair sum of (2, 5) = 7, Pair sum of (3, 4) = 7
Pair sum of (3, 5) = 8, Pair sum of (4, 5) = 9
Maximum sum of a pair is 9 which is due ot pair (4, 5). 
So the output is 1.

Output: 1

Example 2

Input: 
array = [1, 2, 2, 3]

Maximum pair sum = 3
Pairs: (1, 3), (2, 2) = 2 pairs
Output: 2

Approaches for Number of Pairs with Maximum Sum

Here is a list of approaches for the number of pairs with maximum sum which we will be discussing in this article with stepwise explanation and complete example codes.

Using Brute Force Approach

In the brute force approach for number of pairs with maximum sum, we consider all possible pairs in the array and find maximum sum. Then we count the number of pairs that have the same maximum sum.

  • First, we have declared an array arr of integers and then we have defined a function pair() which accepts arr as argument.
  • Then we have declared two variables maxSum and count which stores the maximum sum of pairs and keeps a track of count of pairs with maximum sum respectively.
  • Then we have used a nested for loop where outer loop selects an element from the array to make pair with the elements of inner loop which starts after the next element selected by i in outer loop.
  • The variable sum stores the sum of the pairs formed by outer and inner loop and then this sum is compared with maxSum using if/else statement.
  • If the sum is greater than the maxSum, then the value of maxSum is updated with value of sum. If the value of sum and maxSum are equal then the count is incremented by 1 to get count of pairs with maximum sum.
  • After the completion of loop, the count is returned and is displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for number of pairs with maximum sum using brute force approach.

let arr = [1, 2, 2, 3];
console.log("Given array: ", arr);
function pair(arr) {
    let maxSum = -Infinity;
    let count = 0;

    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            let sum = arr[i] + arr[j];
            if (sum > maxSum) {
                maxSum = sum;
                count = 1;
            } else if (sum === maxSum) {
                count++;
            }
        }
    }
    return count;
}

console.log("Number of pairs with maximum sum: ", pair(arr)); 

Using Sorting

In this approach for number of pairs with maximum sum we have used sorting technique. We sort the array in ascending order and then calculate the sum of two largest numbers. Then we can count the number of pairs that have the same maximum sum.

  • First we have sorted the elements of given array in ascending order using sort() method.
  • Then we calculate the sum of two largest numbers using length property and store it in maxSum.
  • Then we increase the count if the maxSum and sum of pairs are equal.
  • After the completion of loop, the count is returned and is displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for number of pairs with maximum sum using sorting technique.

let arr = [1, 2, 2, 3];
console.log("Given array: ", arr);

function pair(arr) {
    arr.sort((a, b) => a - b);
    let maxSum = arr[arr.length - 1] + arr[arr.length - 2];
    let count = 0;
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i] + arr[j] === maxSum) {
                count++;
            }
        }
    }
    return count;
}

console.log("Number of pairs with maximum sum: ", pair(arr));

Using Hash Table

In this approach we are using hash table for number of pairs with maximum sum. We use a hash table to keep track of the frequency of each element in the array. Then we find the maximum element in the array and count the number of pairs that have the same maximum sum.

  • Create a hash table to store the frequency of each element in the array.
  • Find the maximum element in the array.
  • Find the frequency of the maximum element in the hash table.
  • If the frequency of the maximum element is greater than 1, then the number of pairs with maximum sum is equal to the product of the frequency of the maximum element and (frequency of the maximum element - 1) divided by 2. This is because the number of ways to choose two elements from n elements is equal to n choose 2, which is equal to n*(n-1)/2.
  • If the frequency of the maximum element is equal to 1, then we need to find the second maximum element in the array. To do this, we can iterate through the hash table in descending order and find the second maximum element that has a non-zero frequency.
  • Calculate the number of pairs with the maximum sum using the frequency of the maximum element and the frequency of the second maximum element. The formula for this is the product of the frequency of the maximum element and the frequency of the second maximum element.

Example

Here is a complete example code implementing above mentioned steps for number of pairs with maximum sum using hashing table.

let arr = [1, 2, 2, 3];
console.log("Given array: ", arr);

function pair(arr) {
    let freq = {};
    let max = -Infinity;
    let secondMax = -Infinity;
    for (let i = 0; i < arr.length; i++) {
        freq[arr[i]] = freq[arr[i]] ? freq[arr[i]] + 1 : 1;
        if (arr[i] > max) {
            secondMax = max;
            max = arr[i];
        } else if (arr[i] > secondMax && arr[i] < max) {
            secondMax = arr[i];
        }
    }
    let count;
    if (freq[max] > 1) {
        count = freq[max] * (freq[max] - 1) / 2;
    } else {
        count = freq[max] * freq[secondMax];
    }
    return count;
}

console.log("Number of pairs with maximum sum: ", pair(arr));

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force Approach O(n^2) O(1)
Sorting Technique O(nlogn) O(1)
Hash Table O(n) O(n)

Conclusion

In this article, we discussed Javascript program for number of pairs with maximum sum using three different approaches. These approaches are: by using brute force approach, by using sorting technique and by using hash table. Out of all the three approaches, hash table is the most efficient one.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2025-01-27T11:27:38+05:30

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements