
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The target sum for the two partitions is sum(nums) / 2.
Could you reduce the time complexity if you arbitrarily divide nums into two halves (two arrays)? Meet-in-the-Middle?
For both halves, pre-calculate a 2D array where the kth index will store all possible sum values if only k elements from this half are added.
For each sum of k elements in the first half, find the best sum of n-k elements in the second half such that the two sums add up to a value closest to the target sum from hint 1. These two subsets will form one array of the partition.