#2035
Partition Array Into Two Arrays to Minimize Sum Difference
international master · 2135 · lc hard +32 · verified · 23.1% accepted · 3,753 likes · top 3%
Description
Given an integer array nums with 2 * n elements, split every element into one of two groups each of size n. Return the smallest possible absolute difference between the sums of the two groups.
Example 1:
Input: nums = [3,9,7,3]
Output: 2
Explanation: One optimal partition is: [3,9] and [7,3].
The absolute difference between the sums of the arrays is abs((3 + 9) - (7 + 3)) = 2.
Example 2:
Input: nums = [-36,36]
Output: 72
Explanation: One optimal partition is: [-36] and [36].
The absolute difference between the sums of the arrays is abs((-36) - (36)) = 72.
Example 3:
Input: nums = [2,-1,0,4,-2,-9]
Output: 0
Explanation: One optimal partition is: [2,4,-9] and [-1,0,-2].
The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0.
Code
1
2
3