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.
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.
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.
Accepted:
Submission: