Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Array Partition

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums containing 2n integers.
Your task is to group these integers into n pairs such that the sum of the smaller number in each pair is maximized.
Formally, if the pairs are (a1, b1), (a2, b2), ..., (an, bn),
then you need to maximize:
sum(min(ai, bi)) for all i.
Return the maximum possible sum.

Example 1:

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

Output: 6

Explanation:

All possible pairings (ignoring order) are: 1. (5,2), (9,1) → min(5,2) + min(9,1) = 2 + 1 = 3 2. (5,9), (2,1) → 5 + 1 = 6 ✅ 3.] (5,1), (2,9) → 1 + 2 = 3 Hence, the maximum sum is 6.

Example 2:

Input: nums = [7, 3, 8, 2, 4, 6]

Output: 13

Explanation:

The optimal pairing is (3,2), (4,6), (7,8). min(3,2) + min(4,6) + min(7,8) = 2 + 4 + 7 = 13. Thus, the maximum sum is 13.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result