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 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) = 8

Your 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
Greedy Pairing Strategy VisualizationOriginal Array: [3, 5, 2, 3]3523SortAfter Sorting: [2, 3, 3, 5]2LEFT335RIGHTOptimal Pairs:(2, 5) โ†’ Sum = 7(3, 3) โ†’ Sum = 6Maximum Pair Sum = 7Optimal result achieved!
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

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space for variables (assuming in-place sorting)

n
2n
โœ“ Linear Space

Constraints

  • n == nums.length
  • 2 <= n <= 105
  • n is even
  • 1 <= nums[i] <= 105
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
42.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen