Minimize Maximum Pair Sum in Array

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. 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*.

30:00

Minimize Maximum Pair Sum in Array
medium
Free written solution
Topics
Companies

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.

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.

Example 1:
Input: {"nums":[3,5,2,3]}
Output: 7
Constraints:
  • n==nums.lengthn == \text{nums.length}

  • 2≤n≤1052 \leq n \leq 10^5

  • n is even.

  • 1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,5,2,3]}

We must split 4 numbers into 2 pairs so the largest pair sum is as small as possible. The biggest number has to pair with someone, and the safest partner is the smallest number.

nums (unsorted)

0
3
1
5
2
2
3
3

pair sums (smallest + largest, walking inward)

No pairs yet.

pairs formed

0/2

best (max pair sum)

-

small end (i)
large end (j)
pair formed now
pair with the max sum ★
Variables
VariableValue
i-
j-
nums[i]+nums[j]-
best0
DepthFunction Call
Stack empty
0/4