Achieving maximum possible pair sum in JavaScript



Maximum Possible Pair Sum

The maximum possible pair sum is a traditional problem in Data Structures and Algorithms (DSA). In this problem, we calculate the sum of two elements (i.e., pairs) in an array such that the resulting sum is the maximum among all possible pair sums.

For example, we have an array [1, 2, 3, 4]. The sum of the pair (3, 4) is 7, which is the maximum among all pair sums in the given array.

Problem Statement

We have given an array named nums[], and our task is to write a JavaScript program to achieve the maximum possible pair sum of the given array.

Let's see some input and output scenarios to understand the problem in a better way:

Scenario 1

Input: arr = [1, 4, 3, 2]
Output: 7
Explanation: The sum of the pair (4, 3) is 7, which is the maximum among all the other pair sums in the given array.

Scenario 2

Input: arr = [2, 2, 2, 2]
Output: 4
Explanation: The sum of the pair (2, 2) is 4, which is the maximum and equal to all other pair sums in the given array.

Finding Maximum Possible Pair Sum Using JavaScript

Following are the different ways to find the maximum possible pair sum in the given array using JavaScript:

Using Brute-Force Approach

The brute-force approach is one of the straightforward approaches of Data Structures and Algorithms (DSA) to select out all possible combinations or solutions until it does not meets the given requirement (or condition).

In this case, the brute-force approach calculates the sum of each possible pair within a nested for-loop:

sum = arr[i] + arr[j]

And compares it with the previously stored maximum:

if(sum > maxSum)

It continues this process until all pairs have been checked. Once the maximum pair sum is found, it stops checking and returns the result.

Algorithm

Here is the algorithm to find the maximum possible pairs' sum in the given array:

Step 1: Initialize an array arr[].
Step 2: Check if arr.length <= 1, return -1.
Step 3: Initialize maxSum = -1 (minimum value)
Step 4: Iterate over the array using nested loops and find the sum of each pair (arr[i], arr[j]): sum = arr[i] + arr[j];
Step 5: Compare the sum with maxSum; if sum > maxSum, then update maxSum: maxSum = sum; else, skip.
Step 6: Repeat Steps 3 and 4 until maxSum holds the maximum pair sum. 5 If maxSum != -1, return maxSum

Example

In the following program, we define a function named findPairSum(), which will implement the above algorithm logic to find the maximum possible pair sum in the given array [12, 2, 18, 5]:

//function to calculate maximum pair sum
function pairSum(arr) {
   let length = arr.length;
   if (length <=1) {
      return -1;
   }
   let maxSum = -1;
   let sum = 0;
   for (let i = 0; i < length; i++){
      for (let j = i + 1; j < length; j++){
         //find the sum of all pairs
         sum = arr[i] + arr[j]; 
         //check the sum with previous maxSum if greater update the previous with current maximum sum
         if (sum > maxSum) {
            maxSum = sum;
         }
      }
   }
   
   return (maxSum == -1) ? -1 : maxSum;
}
const arr = [12, 2, 18, 5];
console.log("The given array is: ", arr);
console.log("Maximum possible pair sum: ", pairSum(arr));

The above program displays the following output:

The given array is: [ 12, 2, 18, 5 ] 
Maximum possible pair sum: 30

Using the sort() Method of Array Object

The sort() method is a built-in method of the Array object in JavaScript. It accepts an optional compareFn (comparison function) as a parameter.

If we do not pass the compareFn function by default, this method sorts the given array in ascending order. The compareFn function takes two elements (commonly referred to as a and b) and checks their order as follows:

  • If a - b < 0: a comes before b
  • If a - b > 0: b comes before a
  • If a - b === 0: order remains unchanged (equal values)

Algorithm

Here is the algorithm to find the maximum possible pairs' sum in the given array:

Step 1: Initialize an array arr[].
Step 2: Sort the array in descending order using below method logic: arr.sort((a, b) => b - a);
Step 3: Find the sum of the largest (i.e., 0th element) and second-largest (i.e., 1st element) elements: maxSum = arr[0] + arr[1];
Step 4: Print the maxSum.

Example

The following program uses the above algorithm to find the maximum possible pair sum in the given array [1, 4, 2, 5, 3]:

const arr = [1, 4, 2, 5, 3];
console.log("The given array: ", arr);
//sorting array in descending order
arr.sort((a, b) => b - a); 
console.log("Array after sorting: ", arr);
//find sum of largest element and second largest element
let maxSum = arr[0] + arr[1];
console.log("Maximum possible pair sum: ", maxSum);

Following is the output of the above program:

The given array: [ 1, 4, 2, 5, 3 ] 
Array after sorting: [ 5, 4, 3, 2, 1 ] 
Maximum possible pair sum: 9
Updated on: 2025-08-28T15:57:09+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements