Partition Array Into Two Arrays to Minimize Sum Difference - Problem
You are given an integer array nums of 2 * n integers. You need to partition nums into two arrays of length n to minimize the absolute difference of the sums of the arrays.
To partition nums, put each element of nums into one of the two arrays.
Return the minimum possible absolute difference.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,9,7,3]
›
Output:
2
💡 Note:
Split into [3,9] and [7,3]. Sum difference is |12-10| = 2, which is minimal.
Example 2 — Perfect Split
$
Input:
nums = [-36,36]
›
Output:
72
💡 Note:
Only two elements, must split as [-36] and [36]. Difference is |-36-36| = 72.
Example 3 — Multiple Options
$
Input:
nums = [2,-1,0,4,-2,-9]
›
Output:
0
💡 Note:
Can split as [2,-1,-2] and [0,4,-9] both summing to -1. Difference is |-1-(-1)| = 0.
Constraints
- 1 ≤ n ≤ 15
- nums.length == 2 * n
- -107 ≤ nums[i] ≤ 107
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code