Skip to content
Onsite

Array Partition

EasyP3Coding

Given an array nums of 2N integers, split it into N pairs. The value of a pair is its smaller element. Return the largest possible sum of the N pair values.

Example 1

Input
nums = [1,4,3,2]
Output
4
Why
Pair (1,2) and (3,4): 1 + 3 = 4.

Example 2

Input
nums = [6,2,6,5,1,2]
Output
9
Why
Pairs (1,2), (2,5), (6,6) give 1 + 2 + 6.

Constraints

  • 2 <= nums.length <= 2 * 10^4, and the length is even
  • -10^4 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 9.