Array Partition

Given an integer array `nums` of `2n` integers, group them into `n` pairs `(a1, b1), (a2, b2), ..., (an, bn)` so that the sum of `min(ai, bi)` over all pairs is as large as possible. Return that maximum sum.

30:00

Array Partition
easy
Free written solution
Topics
Companies

Given an integer array nums of 2n integers, group them into n pairs (a1, b1), (a2, b2), ..., (an, bn) so that the sum of min(ai, bi) over all pairs is as large as possible. Return that maximum sum.

Example 1:
Input: {"nums":[1,4,3,2]}
Output: 4
Constraints:
  • 1≤n≤1041 \leq n \leq 10^4

  • nums.length==2n\text{nums.length} == 2n

  • −104≤nums[i]≤104-10^4 \leq \text{nums}[i] \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,4,3,2]}

4 numbers must become 2 pairs; each pair only scores its smaller number.

input nums

1
4
3
2

sorted, then paired with the neighbour

kept = min

given up

not sorted yet

total of kept minimums

0

0

Variables
VariableValue
n2
DepthFunction Call
Stack empty
0/5