Loading...
You are given an integer array nums holding 2n values. Split all of them into n pairs, using every value exactly once.
Each pair contributes its smaller value to the total (if the two values are equal, that value). Return the largest total achievable over all ways of pairing.
Input: nums = [1,4,3,2]
Output: 4
Explanation: Pairing as (1,2) and (3,4) contributes 1 + 3 = 4. The alternatives are worse: (1,3) and (2,4) gives 1 + 2 = 3, and (1,4) and (2,3) gives 1 + 2 = 3.
Input: nums = [6,2,6,5,1,2]
Output: 9
Explanation: Pairing as (1,2), (2,5) and (6,6) contributes 1 + 2 + 6 = 9.
nums.length =2nnums[i] ≤104Click "Run" to test with sample cases or "Submit" to run all tests.