Partition Array Into Two Arrays to Minimize Sum Difference - Problem

You're given an integer array nums of 2n integers. Your task is to partition this array into two separate arrays, each containing exactly n elements, such that the absolute difference between the sums of these two arrays is minimized.

Think of it as trying to balance two scales as evenly as possible - you want to distribute the weights (numbers) so that neither side is much heavier than the other.

Goal: Find the minimum possible absolute difference between the sums of the two partitioned arrays.

Example: If nums = [3,9,7,3], you could partition it as [3,7] and [9,3], giving sums of 10 and 12 respectively, for a difference of 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: [3,9,7,3]
โ€บ Output: 2
๐Ÿ’ก Note: We can partition into [3,7] and [9,3]. The sums are 10 and 12 respectively, giving an absolute difference of 2.
example_2.py โ€” Larger Array
$ Input: [-36,36]
โ€บ Output: 72
๐Ÿ’ก Note: We must partition into [-36] and [36]. The sums are -36 and 36 respectively, giving an absolute difference of 72.
example_3.py โ€” All Same Elements
$ Input: [2,2,2,2]
โ€บ Output: 0
๐Ÿ’ก Note: We can partition into [2,2] and [2,2]. Both sums are 4, giving an absolute difference of 0.

Visualization

Tap to expand
Balancing the Scales - Meet in the Middle3+7=109+3=12Difference: 2Meet in the Middle StrategyStep 1: Split array [3,9,7,3]39|73Left HalfRight HalfStep 2: Generate all subset sumsLeft: {โˆ…:0, {3}:3, {9}:9, {3,9}:12}Right: {โˆ…:0, {7}:7, {3}:3, {7,3}:10}Step 3: Match subsets optimallyChoose {3} from left + {7} from right = 10Remaining: {9} + {3} = 12Difference: |10 - 12| = 2Complexity AnalysisBrute Force: O(4n)Meet-in-Middle: O(3n)Space: O(2n)Much faster for large n!
Understanding the Visualization
1
Items to Balance
You have 2n items with different weights that need to be distributed evenly
2
Split Strategy
Instead of trying all combinations, divide items into two groups and analyze each separately
3
Smart Matching
Use binary search to efficiently find the best way to combine subsets from both groups
4
Optimal Balance
Find the combination that minimizes the absolute weight difference between the two scales
Key Takeaway
๐ŸŽฏ Key Insight: Meet-in-the-middle reduces exponential complexity by splitting the problem space and using binary search for efficient matching, making it practical for larger inputs than brute force approaches.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(3^n)

We generate 2^n subsets for each half, then for each subset in left half, we binary search in right half

n
2n
โœ“ Linear Growth
Space Complexity
O(2^n)

We store all possible subset sums for both halves

n
2n
โš  Quadratic Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 20
  • nums.length is even
  • -106 โ‰ค nums[i] โ‰ค 106
  • The array contains exactly 2n elements
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
38.0K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen