Array Pair Sum

EasyArraySortingGreedy

Description

Given an integer array nums of length 2n, your task is to group these integers into n pairs (a₁, b₁), (a₂, b₂), …, (aₙ, bₙ) such that the sum of min(aᵢ, bᵢ) for i = 1 to n is maximized. Return the maximized sum.

Examples

Input:nums = [1,4,3,2]
Output:4
Explanation:

Sort to [1,2,3,4]; pair (1,2) and (3,4); min sum = 1 + 3 = 4.

Input:nums = [6,2,6,5,1,2]
Output:9
Explanation:

Sort to [1,2,2,5,6,6]; pair (1,2)(2,5)(6,6); min sum = 1+2+6 = 9.

Input:nums = [1,1]
Output:1
Explanation:

Only one pair (1,1); min = 1.

Input:nums = [-1,-2,-3,-4]
Output:-6
Explanation:

Sort to [-4,-3,-2,-1]; pair (-4,-3)(-2,-1); min sum = -4 + -2 = -6.

Constraints

  • 1 ≤ n ≤ 10⁴
  • nums.length == 2 * n
  • -10⁴ ≤ nums[i] ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.