1877. Minimize Maximum Pair Sum in Array
Description
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.
<li>For example, if we have pairs <code>(1,5)</code>, <code>(2,3)</code>, and <code>(4,4)</code>, the <strong>maximum pair sum</strong> would be <code>max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8</code>.</li>
Given an array nums
of even length n
, pair up the elements of nums
into n / 2
pairs such that:
<li>Each element of <code>nums</code> is in <strong>exactly one</strong> pair, and</li>
<li>The <strong>maximum pair sum </strong>is <strong>minimized</strong>.</li>
Return 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:
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>n</code> is <strong>even</strong>.</li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
Solutions
Solution 1: Greedy
To minimize the maximum pair sum in the array, we can pair the smallest number with the largest number, the second smallest with the second largest, and so on.
Therefore, we can first sort the array, then use two pointers to point to the two ends of the array. Calculate the sum of the numbers pointed to by the two pointers, update the maximum pair sum, then move the left pointer one step to the right and the right pointer one step to the left. Continue this process until the two pointers meet, and we will get the minimum maximum pair sum.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
Python3
class Solution:
def minPairSum(self, nums: List[int]) -> int:
nums.sort()
return max(x + nums[-i - 1] for i, x in enumerate(nums[: len(nums) >> 1]))
Java
class Solution {
public int minPairSum(int[] nums) {
Arrays.sort(nums);
int ans = 0, n = nums.length;
for (int i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - i - 1]);
}
return ans;
}
}
C++
class Solution {
public:
int minPairSum(vector<int>& nums) {
ranges::sort(nums);
int ans = 0, n = nums.size();
for (int i = 0; i < n >> 1; ++i) {
ans = max(ans, nums[i] + nums[n - i - 1]);
}
return ans;
}
};
Go
func minPairSum(nums []int) (ans int) {
sort.Ints(nums)
n := len(nums)
for i, x := range nums[:n>>1] {
ans = max(ans, x+nums[n-1-i])
}
return
}
TypeScript
function minPairSum(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - 1 - i]);
}
return ans;
}
Rust
impl Solution {
pub fn min_pair_sum(nums: Vec<i32>) -> i32 {
let mut nums = nums;
nums.sort();
let mut ans = 0;
let n = nums.len();
for i in 0..n / 2 {
ans = ans.max(nums[i] + nums[n - i - 1]);
}
ans
}
}
JavaScript
/**
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function (nums) {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i < n >> 1; ++i) {
ans = Math.max(ans, nums[i] + nums[n - 1 - i]);
}
return ans;
};
C#
public class Solution {
public int MinPairSum(int[] nums) {
Array.Sort(nums);
int ans = 0, n = nums.Length;
for (int i = 0; i < n >> 1; ++i) {
ans = Math.Max(ans, nums[i] + nums[n - i - 1]);
}
return ans;
}
}