Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Achieving maximum possible pair sum in JavaScript
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
<b>Input:</b> arr = [1, 4, 3, 2] <b>Output:</b> 7 <b>Explanation:</b> 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
<b>Input:</b> arr = [2, 2, 2, 2] <b>Output:</b> 4 <b>Explanation:</b> 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 meets the given requirement.
In this case, the brute-force approach calculates the sum of each possible pair within nested for-loops and compares it with the previously stored maximum. It continues this process until all pairs have been checked.
Algorithm
<b>Step 1:</b> Initialize an array arr[]. <b>Step 2:</b> Check if arr.length < 2, return -1. <b>Step 3:</b> Initialize maxSum = -Infinity (minimum value) <b>Step 4:</b> Iterate over the array using nested loops and find the sum of each pair (arr[i], arr[j]): sum = arr[i] + arr[j]; <b>Step 5:</b> Compare the sum with maxSum; if sum > maxSum, then update maxSum: maxSum = sum; <b>Step 6:</b> Repeat until maxSum holds the maximum pair sum and return maxSum.
Example
In the following program, we define a function named pairSum(), which implements 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 using brute force
function pairSum(arr) {
let length = arr.length;
if (length < 2) {
return -1;
}
let maxSum = -Infinity;
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 and update if greater
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
const arr = [12, 2, 18, 5];
console.log("The given array is:", arr);
console.log("Maximum possible pair sum:", pairSum(arr));
The given array is: [ 12, 2, 18, 5 ] Maximum possible pair sum: 30
The time complexity of this approach is O(n²) as we use nested loops to check all possible pairs.
Using sort() Method
The sort() method provides a more efficient approach. By sorting the array in descending order, the first two elements will be the largest values, giving us the maximum possible pair sum directly.
Algorithm
<b>Step 1:</b> Initialize an array arr[]. <b>Step 2:</b> Check if arr.length < 2, return -1. <b>Step 3:</b> Sort the array in descending order: arr.sort((a, b) => b - a); <b>Step 4:</b> Find the sum of the largest and second-largest elements: maxSum = arr[0] + arr[1]; <b>Step 5:</b> Return 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]:
function maxPairSumOptimized(arr) {
if (arr.length < 2) {
return -1;
}
// Sort array in descending order
arr.sort((a, b) => b - a);
console.log("Array after sorting:", arr);
// Find sum of largest and second largest elements
let maxSum = arr[0] + arr[1];
return maxSum;
}
const arr = [1, 4, 2, 5, 3];
console.log("The given array:", arr);
console.log("Maximum possible pair sum:", maxPairSumOptimized([...arr]));
The given array: [ 1, 4, 2, 5, 3 ] Array after sorting: [ 5, 4, 3, 2, 1 ] Maximum possible pair sum: 9
The time complexity of this approach is O(n log n) due to the sorting operation, which is more efficient than the brute-force approach for larger arrays.
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n²) | O(1) | Small arrays |
| Sort Method | O(n log n) | O(1) | Large arrays |
Conclusion
The maximum possible pair sum can be found efficiently using the sort method with O(n log n) complexity. For optimal performance with larger datasets, prefer the sorting approach over the brute-force method.
