Pair up array elements to maximize the sum of pair minimums

Quick Overview

Split an array of 2n integers into n pairs so that the sum of each pair's smaller value is as large as possible, and return that sum. It tests whether you can find the best pairing and argue that no other pairing beats it, including with negative values and duplicates.

Pair up array elements to maximize the sum of pair minimums

Company: Adobe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums` of `2n` integers, group these integers into `n` pairs `(a1, b1), (a2, b2), ..., (an, bn)` such that the sum of `min(ai, bi)` over all `i` is maximized. Return the maximized sum. ### Function Signature ```python def max_sum_of_pair_mins(nums: list[int]) -> int: ``` ### Rules - Every element belongs to exactly one pair. Equal values at different positions are separate elements and may be paired with each other. - Return only the sum, not the pairs. ### Constraints - `1 <= n <= 10^4` - `nums.length == 2 * n` - `-10^4 <= nums[i] <= 10^4` ### Examples ```text Input: nums = [1,4,3,2] Output: 4 ``` Sorted, the values are `[1, 2, 3, 4]`. The pairs `(1, 2)` and `(3, 4)` give `1 + 3 = 4`, while the other two pairings give `1 + 2 = 3`. ```text Input: nums = [6,2,6,5,1,2] Output: 9 ``` Sorted, the values are `[1, 2, 2, 5, 6, 6]`. The pairs `(1, 2)`, `(2, 5)` and `(6, 6)` give `1 + 2 + 6 = 9`. ```text Input: nums = [-1,-5,3,0] Output: -5 ``` The pairs `(-5, -1)` and `(0, 3)` give `-5 + 0 = -5`. The other pairings, `(-5, 0)` with `(-1, 3)` and `(-5, 3)` with `(-1, 0)`, both give -6.

Overview: Split an array of 2n integers into n pairs so that the sum of each pair's smaller value is as large as possible, and return that sum. It tests whether you can find the best pairing and argue that no other pairing beats it, including with negative values and duplicates.

Adobe logo
Adobe
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) over all i is maximized.

Return the maximized sum.

Function Signature

def max_sum_of_pair_mins(nums: list[int]) -> int:

Rules

  • Every element belongs to exactly one pair. Equal values at different positions are separate elements and may be paired with each other.
  • Return only the sum, not the pairs.

Constraints

  • 1 <= n <= 10^4
  • nums.length == 2 * n
  • -10^4 <= nums[i] <= 10^4

Examples

Input: nums = [1,4,3,2]
Output: 4

Sorted, the values are [1, 2, 3, 4]. The pairs (1, 2) and (3, 4) give 1 + 3 = 4, while the other two pairings give 1 + 2 = 3.

Input: nums = [6,2,6,5,1,2]
Output: 9

Sorted, the values are [1, 2, 2, 5, 6, 6]. The pairs (1, 2), (2, 5) and (6, 6) give 1 + 2 + 6 = 9.

Input: nums = [-1,-5,3,0]
Output: -5

The pairs (-5, -1) and (0, 3) give -5 + 0 = -5. The other pairings, (-5, 0) with (-1, 3) and (-5, 3) with (-1, 0), both give -6.