Maximize Paired Sum with Coverage Constraint
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests greedy reasoning and sorting to maximize the sum of "lesser" elements across a constrained pairing of an even-length array.
Problem
You are given an array of n integers, where n is always even. Your task is to partition all n elements into exactly n/2 pairs, then designate one element in each pair as the leader and the other as the supporter.
Within every pair, the supporter's value must be greater than or equal to the leader's value:
value[supporter] >= value[leader]
Each element belongs to exactly one pair and plays exactly one role.
The total score of a partition is the sum of all leaders' values:
score = sum of value[leader] over all pairs
Find the maximum possible total score across all valid ways to partition and assign roles.
Constraints
2 <= n <= 2 * 10^5, andnis always even.1 <= values[i] <= 10^9- Your solution should run in significantly better than
O(n^2)time.
Examples
Example 1
Input: max_paired_score([1, 2])
Output: 1
Example 2
Input: max_paired_score([1, 3, 2, 4])
Output: 4
Example 3
Input: max_paired_score([5, 5, 5, 5])
Output: 10
Hints
Hint 1
Notice that within any valid pair the leader is always the smaller of the two values — so you are really maximizing the sum of pair minimums.
Hint 2
Think about what happens when you sort the array. Which arrangement of pairs avoids "wasting" large values on leader slots that smaller values could fill just as well?
Hint 3
After sorting, try pairing up neighbors: (index 0, index 1), (index 2, index 3), and so on. The leader in each pair is the element at the even index. Can you convince yourself no other pairing can do better?
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key reframing
Within any valid pair, the supporter must be >= the leader, so the leader is always the smaller of the two values. Maximizing the total score is therefore equivalent to maximizing the sum of minimums across all pairs.
Greedy strategy
Sort the array in ascending order, then form pairs from consecutive neighbors: (v[0], v[1]), (v[2], v[3]), and so on. The leader (minimum) of each pair sits at even indices 0, 2, 4, …, so the answer is sum(sorted_vals[::2]).
Why this is optimal
Take any valid partition and list its leaders in ascending order: p1 <= p2 <= ... <= p_{n/2}. Focus on the i-th leader p_i.
- There are
ileaders all<= p_i, and each one has a distinct supporter>= p_i(because supporter >= leader >=p_ifor all leaders up top_i).
Wait — let's state the bound the other way, which is cleaner:
p_ialong withp_1, ..., p_{i-1}areivalues that are each<= p_i.- More usefully: the
ileadersp_i, ..., p_{n/2}are each>= p_i, and each has a distinct supporter also>= p_i. That gives2*(n/2 - i + 1)distinct values all>= p_i. - Therefore
p_i <= v[2*(i-1)](the element at even index2i-2in the sorted array).
So every valid partition satisfies p_i <= sorted_vals[2*(i-1)], meaning the greedy assignment achieves the tightest possible value at every position. The greedy pairing is optimal.
Exchange argument (alternative). If the largest element is paired with anything other than the second-largest, you can swap partners so it covers the second-largest instead. This never decreases the sum of minimums. Applying this argument inductively proves the adjacent-pair strategy is optimal.
Intuition. The largest value can never be a leader in a useful way — it will always serve as a supporter. The best use of it is to "cover" the second-largest, which then contributes its full value to the score. Repeat on the remaining elements.
Edge cases
n = 2: One pair, answer ismin(values).- All elements equal: Any pairing is valid; answer is
(n/2) * value. - Duplicates: The
>=constraint allows equal values to pair together, handled naturally by sorting. - Large sums: Up to
10^5leaders at10^9each yields ~10^14. Python handles this natively; uselong/long longin Java/C++.
Complexity
- Time:
O(n log n)for sorting,O(n)for the summation. - Space:
O(n)for the sorted copy;O(1)extra if in-place sorting is allowed.
Solution
from typing import List
def max_paired_score(values: List[int]) -> int:
# Sort ascending; adjacent pairs are (v[0],v[1]), (v[2],v[3]), ...
# The leader of each pair is the smaller element at even indices 0, 2, 4, ...
sorted_vals = sorted(values)
return sum(sorted_vals[::2])
sorted_vals[::2] selects indices 0, 2, 4, … — the minimum of each consecutive pair — and sums them in a single pass.