Minimize Maximum Pair Sum in Array - Problem
Imagine you're organizing a dance competition where dancers need to be paired up optimally! ๐๐บ
You have an array of
The pair sum of a pair
For example, with pairs
โข Pair sums:
โข Maximum pair sum =
Your goal is to minimize this maximum pair sum by choosing the optimal pairing strategy. Return the smallest possible maximum pair sum.
You have an array of
nums with an even length n, and you need to pair up all elements into n/2 pairs. Each element must be used exactly once.The pair sum of a pair
(a, b) equals a + b. The maximum pair sum is the largest sum among all pairs.For example, with pairs
(1,5), (2,3), and (4,4):โข Pair sums:
1+5=6, 2+3=5, 4+4=8โข Maximum pair sum =
max(6, 5, 8) = 8Your goal is to minimize this maximum pair sum by choosing the optimal pairing strategy. Return the smallest possible maximum pair sum.
Input & Output
example_1.py โ Basic Example
$
Input:
[3,5,2,3]
โบ
Output:
7
๐ก Note:
After sorting: [2,3,3,5]. Optimal pairs: (2,5) with sum 7, and (3,3) with sum 6. The maximum pair sum is 7.
example_2.py โ All Same Elements
$
Input:
[3,5,4,2,4,6]
โบ
Output:
8
๐ก Note:
After sorting: [2,3,4,4,5,6]. Optimal pairs: (2,6)=8, (3,5)=8, (4,4)=8. All pairs have sum 8, so maximum is 8.
example_3.py โ Minimum Case
$
Input:
[1,2]
โบ
Output:
3
๐ก Note:
Only one pair possible: (1,2) with sum 3. The maximum (and only) pair sum is 3.
Visualization
Tap to expand
Understanding the Visualization
1
Sort Elements
Arrange all elements in ascending order to identify extremes
2
Pair Extremes
Match smallest with largest to create balanced pairs
3
Continue Inward
Move pointers inward, continuing the pairing strategy
4
Find Maximum
Track the largest sum among all created pairs
Key Takeaway
๐ฏ Key Insight: Pairing extremes (smallest with largest) minimizes the maximum pair sum through greedy optimization.
Time & Space Complexity
Time Complexity
O(n log n)
Dominated by sorting the array. Pairing takes O(n) time
โก Linearithmic
Space Complexity
O(1)
Only using constant extra space for variables (assuming in-place sorting)
โ Linear Space
Constraints
-
n == nums.length -
2 <= n <= 105 -
nis even -
1 <= nums[i] <= 105
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code