Array Partition

Easy

Topics
ArrayGreedySorting

Given an array nums of 2n integers, group them into n pairs so that the sum of min(pair) over all pairs is maximized, and return that maximum sum.

Example 1

sorted1234
pairs(1, 2)(3, 4)
the min of each pair, which is what counts
Only the smaller value of each pair is scored, so the goal is to waste as little as possible on the discarded halves. Sorting and pairing neighbours does exactly that, giving 4.
Top row is the sorted input; chips are the chosen pairs.
Input:  nums = [1,4,3,2]
Output: 4
Explanation: Pairs (1,2) and (3,4) give 1 + 3 = 4.

Example 2

sorted122566
pairs(1, 2)(2, 5)(6, 6)
the min of each pair, which is what counts
Input:  nums = [6,2,6,5,1,2]
Output: 9

Constraints

  • 1 <= n <= 10^4
  • nums.length == 2n
  • -10^4 <= nums[i] <= 10^4
Editorial — intuition, approach, complexity and pitfalls

Intuition

We split 2n numbers into n pairs and add up min of each pair. Every number is used, but only half of them — the smaller of each pair — actually count toward the score. So the real question is which n numbers get to be minimums, and we want that set to be as large as possible.

The brute force is to try every pairing, which is a double factorial and immediately hopeless. But notice what pairing costs us: whenever we pair a < b, the number b is thrown away entirely. The waste is b - a. To lose as little as possible, pair each number with the value closest to it — and after sorting, the closest value is the neighbour.

So sort and pair adjacent elements: (nums[0], nums[1]), (nums[2], nums[3]), .... The minimum of each pair is the element at the even index, so the answer is the sum of nums[0], nums[2], nums[4], ...

Why this is optimal, briefly: the largest element can never contribute, so it should destroy as little as possible, which means pairing it with the second largest. Removing both leaves the same problem on a smaller array, and the argument repeats. That is an exchange argument, and it is the thing to say out loud rather than asserting the greedy works.

Approach

  1. Sort ascending.
  2. Sum every element at an even index.

Dry Run

nums = [6,2,6,5,1,2]
sorted              [1, 2, 2, 5, 6, 6]
pairs               (1,2) (2,5) (6,6)
minimums             1     2     6      -> 9

a worse pairing      (1,6) (2,6) (2,5)
minimums              1     2     2     -> 5

nums = [1,4,3,2]
sorted              [1, 2, 3, 4]
pairs               (1,2) (3,4)  -> 1 + 3 = 4

Complexity

  • Time — O(n log n): the sort dominates; the sum is one linear pass.
  • Space — O(log n) in C++ and O(n) in Java and Python, not O(1): the sum itself needs nothing, but the sort does, and "O(1) space" is the claim a sorting step makes false. Arrays.sort(int[]) in Java and std::sort in C++ are in-place quicksort variants that still recurse, so O(log n) of stack; Python's list.sort() is Timsort and allocates an O(n) merge buffer. Note also that writing the sum as sum(nums[::2]) would materialise n/2 elements on its own — the strided generator above avoids that, but the sort's cost remains.

Common Pitfalls

  • Claiming O(1) space because the loop uses no extra variables. The sort is part of the algorithm and part of its space cost.
  • Taking nums[1::2] — the odd indices are the maximums of each pair and are exactly the values discarded.
  • Pairing largest with smallest, which feels balanced and is the worst choice: it throws away the biggest possible gap each time, as the second pairing in the dry run shows.
  • Sorting descending and then summing even indices. That sums the maximums; descending needs the odd indices instead.
  • Assuming negative values break the argument. They do not — the exchange argument is about ordering, not sign, and the constraints allow values down to -10^4.

Full Solution — all languages

class Solution:
    def arrayPairSum(self, nums):
        nums.sort()
        # A generator with a stride, not nums[::2]: the slice would materialise n/2
        # elements. The sort is still the dominant space cost -- see the complexity note.
        return sum(nums[i] for i in range(0, len(nums), 2))
SensAI
Beta
Tap the mic to stop voice mode