Maximize the Sum of Pair Minima

Quick Overview

Maximize the Sum of Pair Minima. Partition the 2n elements of nums into n pairs, using every occurrence once.

Maximize the Sum of Pair Minima

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Partition the 2n elements of nums into n pairs, using every occurrence once. Return the greatest possible sum of the smaller value in each pair. ```cpp class Solution { public: int arrayPairSum(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [8,1,7,2] Output: 8 Explanation: Pair (1,2) and (7,8), giving 1+7. ``` **Example 2:** ```text nums = [-4,-1,2,3] Output: -2 Explanation: Adjacent sorted pairs contribute -4+2. ``` ### Constraints - For n in 1..10000 there are exactly 2n input entries. Values are in -10000..10000. ### Clarifying Questions Use all occurrences exactly once and justify why pairing the two smallest remaining values is safe. ```hint Read the boundary condition Can a pairing of the smallest two values be exchanged into any optimum? ``` ### Contract considerations Use all occurrences exactly once and justify why pairing the two smallest remaining values is safe.

Overview: Maximize the Sum of Pair Minima. Partition the 2n elements of nums into n pairs, using every occurrence once.

Microsoft logo
Microsoft
Sep 13, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Partition the 2n elements of nums into n pairs, using every occurrence once. Return the greatest possible sum of the smaller value in each pair.

class Solution {
public:
    int arrayPairSum(vector<int>& nums);
};

Examples

Example 1:

nums = [8,1,7,2]
Output: 8
Explanation: Pair (1,2) and (7,8), giving 1+7.

Example 2:

nums = [-4,-1,2,3]
Output: -2
Explanation: Adjacent sorted pairs contribute -4+2.

Constraints

  • For n in 1..10000 there are exactly 2n input entries. Values are in -10000..10000.

Clarifying Questions Guidance

Use all occurrences exactly once and justify why pairing the two smallest remaining values is safe.

Contract considerations

Use all occurrences exactly once and justify why pairing the two smallest remaining values is safe.