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.
(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 array nums
of even length n
, pair up the elements of nums
into n / 2
pairs such that:
nums
is in exactly one pair, andReturn the minimized maximum pair sum after optimally pairing up the elements.
Example 1:
Input: nums = [3,5,2,3] Output: 7 Explanation: The elements can be paired up into pairs (3,3) and (5,2). The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
Example 2:
Input: nums = [3,5,4,2,4,6] Output: 8 Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2). The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
Constraints:
n == nums.length
2 <= n <= 105
n
is even.1 <= nums[i] <= 105
Given an array nums
of even length n
, pair up the elements of nums
into n/2
pairs such that each element is in exactly one pair, and the maximum pair sum is minimized. Return the minimized maximum pair sum.
Example 1:
Input: nums = [3,5,2,3] Output: 7 Explanation: The pairs are (3,3) and (5,2), and the maximum pair sum is max(3+3, 5+2) = 7.
Example 2:
Input: nums = [3,5,4,2,4,6] Output: 8 Explanation: The pairs are (2,6), (3,5), (4,4), and the maximum pair sum is max(8, 8, 8) = 8.
The optimal strategy to minimize the maximum pair sum is to pair the smallest element with the largest element, the second smallest with the second largest, and so on. This ensures that the sum of each pair is as close as possible to the average of the elements.
Algorithm:
nums
in ascending order.left
and right
, initialized to the beginning and end of the sorted array, respectively.nums[left]
and nums[right]
.maxPairSum
variable with the maximum of the current maxPairSum
and the current pair sum.left
and decrement right
to consider the next pair.left
and right
cross each other.maxPairSum
.Python:
def minPairSum(nums):
nums.sort()
left = 0
right = len(nums) - 1
maxPairSum = 0
while left < right:
maxPairSum = max(maxPairSum, nums[left] + nums[right])
left += 1
right -= 1
return maxPairSum
Java:
import java.util.Arrays;
class Solution {
public int minPairSum(int[] nums) {
Arrays.sort(nums);
int left = 0;
int right = nums.length - 1;
int maxPairSum = 0;
while (left < right) {
maxPairSum = Math.max(maxPairSum, nums[left] + nums[right]);
left++;
right--;
}
return maxPairSum;
}
}
C++:
#include <algorithm>
#include <vector>
class Solution {
public:
int minPairSum(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
int left = 0;
int right = nums.size() - 1;
int maxPairSum = 0;
while (left < right) {
maxPairSum = std::max(maxPairSum, nums[left] + nums[right]);
left++;
right--;
}
return maxPairSum;
}
};
JavaScript:
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function(nums) {
nums.sort((a, b) => a - b);
let left = 0;
let right = nums.length - 1;
let maxPairSum = 0;
while (left < right) {
maxPairSum = Math.max(maxPairSum, nums[left] + nums[right]);
left++;
right--;
}
return maxPairSum;
};
These code examples all follow the same algorithm and achieve the same result. The choice of language depends on your preferences and the context of the problem. All have the same time and space complexity as analyzed above.