Minimize Maximum Pair Sum in Array - Problem
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 array nums of even length n, pair up the elements of nums into n / 2 pairs such that:
- Each element of
numsis in exactly one pair, and - The maximum pair sum is minimized.
Return the minimized maximum pair sum after optimally pairing up the elements.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,5,2,4]
›
Output:
7
💡 Note:
After sorting [2,3,4,5], pair (2,5) and (3,4). Maximum pair sum is max(7,7) = 7
Example 2 — Larger Array
$
Input:
nums = [3,5,4,2,4,6]
›
Output:
8
💡 Note:
After sorting [2,3,4,4,5,6], pair (2,6), (3,5), (4,4). Maximum pair sum is max(8,8,8) = 8
Example 3 — Minimum Size
$
Input:
nums = [1,4]
›
Output:
5
💡 Note:
Only one pair possible: (1,4) with sum 5
Constraints
- 2 ≤ nums.length ≤ 105
- nums.length is even
- 1 ≤ nums[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array of even length that needs pairing
2
Sort & Pair
Sort array and pair smallest with largest
3
Find Maximum
Return the maximum sum among all pairs
Key Takeaway
🎯 Key Insight: Pairing smallest with largest elements minimizes the maximum pair sum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code