You are given an integer array nums of 2 * n integers. Partition nums into two arrays of length n so that the absolute difference of the sums of the two arrays is as small as possible. To partition nums, put each element of nums into exactly one of the two arrays.
Return the minimum possible absolute difference.
Function Signature
def minimum_difference(nums: list[int]) -> int:
Rules
-
Each element goes into exactly one of the two arrays, and each array gets exactly
n
elements. Order inside the arrays does not matter.
-
Return only the minimum absolute difference of the two sums, as an integer.
Constraints
-
1 <= n <= 15
-
len(nums) == 2 * n
-
-10^7 <= nums[i] <= 10^7
Examples
Input: nums = [3, 9, 7, 3]
Output: 2
One optimal partition is [3, 9] and [7, 3], with sums 12 and 10. The other choices are [3, 7] with [9, 3] (difference 2) and [3, 3] with [9, 7] (difference 10).
Input: nums = [-36, 36]
Output: 72
The only partition is [-36] and [36], with sums -36 and 36.
Input: nums = [1, 2, 3, 4, 5, 100]
Output: 91
The array holding 100 also needs two more elements, and the smallest pair is 1 and 2, so the best split is [100, 1, 2] (sum 103) against [3, 4, 5] (sum 12). Putting 100 alone on one side would give 85, but that violates the equal-length rule.