1344. Partition Into Two Arrays to Minimize Sum Diff

MediumBit ManipulationBitmaskSubset SumMeet in the Middle

Given an array of 2n integers (values may be negative), partition it into two arrays of exactly n elements each. Return the minimum possible absolute difference between the sums of the two arrays.

Input: A JSON object {"nums": [<integers>]} whose length is even.

Output: Return the minimum absolute difference between the two equal-size halves.

Examples

Example 1
Input: {"nums":[3,9,7,3]}
Output: 2
Explanation: Split into {3,9} and {7,3} for sums 12 and 10 -> 2.
Example 2
Input: {"nums":[-36,36]}
Output: 72
Explanation: Each side takes one element -> |(-36) - 36| = 72.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →