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 nums is 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
Minimize Maximum Pair Sum in Array INPUT Original Array nums[] 3 idx 0 5 idx 1 2 idx 2 4 idx 3 Input Details nums = [3, 5, 2, 4] n = 4 (even length) Goal Create n/2 = 2 pairs Each element in one pair Minimize the maximum pair sum ALGORITHM STEPS 1 Sort Array [2, 3, 4, 5] 2 3 4 5 2 Two Pointer Setup left=0, right=n-1 2 L 3 4 5 R 3 Pair: smallest+largest Pair1: (2,5) sum=7 Pair2: (3,4) sum=7 4 Track Max Sum max(7, 7) = 7 Pair Sums: 2+5 = 7 3+4 = 7 FINAL RESULT Optimal Pairing Pair 1 2 + 5 = 7 Pair 2 3 + 4 = 7 Maximum Pair Sum 7 Output: 7 OK - Minimized! Key Insight: Greedy Strategy: Sort the array, then pair the smallest element with the largest element. This balances pair sums and minimizes the maximum. Pairing small with small leaves large+large pairs, which creates unnecessarily high maximum pair sums. Time: O(n log n), Space: O(1) TutorialsPoint - Minimize Maximum Pair Sum in Array | Greedy - Sort and Pair Optimally
Asked in
Google 12 Facebook 8 Amazon 6
89.2K Views
Medium Frequency
~15 min Avg. Time
1.5K 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