hardBit ManipulationPure DSA~45 min
Split into Two Equal-Size Arrays Minimizing Sum Gap
You must divide 2m measurements into two arrays of exactly m elements each, balancing them so their totals are as close as possible. Minimize the absolute difference of the two sums.
Problem
Given an array nums of 2*m integers, partition it into two arrays each of length m. Return the minimum possible absolute difference between the sums of the two arrays.
Input
An integer array nums of even length 2*m.
Output
An integer: the minimum absolute difference of the two equal-size partition sums.
Constraints
- 1 <= m <= 15 (so nums.length is up to 30)
- -10^7 <= nums[i] <= 10^7
Examples
Example 1
Input
nums = [3,9,7,3]
Output
2
Split into [3,9] and [7,3]: sums 12 and 10, difference 2.
Example 2
Input
nums = [-36,36]
Output
72
The only split is [-36] and [36]: difference 72.