Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created January 24, 2026 17:35
Show Gist options
  • Select an option

  • Save tatsuyax25/f07a35af563a98aec1cb77b2d89932da to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/f07a35af563a98aec1cb77b2d89932da to your computer and use it in GitHub Desktop.
The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs. For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8. Given an arr
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function(nums) {
// Step 1: Sort the array so we can pair smallest with largest
nums.sort((a, b) => a - b);
let left = 0; // pointer to smallest element
let right = nums.length - 1; // pointer largest element
let maxPairSum = 0; // track the maximum pair sum we create
// Step 2: Pair elements from both end moving inward
while (left < right) {
const pairSum = nums[left] + nums[right];
// Update the maximum pair sum seen so far
if (pairSum > maxPairSum) {
maxPairSum = pairSum;
}
// Move pointers inward to form the next pair
left++;
right--;
}
// Step 3: Return the minimized maximum pair sum
return maxPairSum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment