Minimum Difference in Sums After Removal of Elements - Problem

You are given a 0-indexed integer array nums consisting of 3 * n elements.

You are allowed to remove any subsequence of elements of size exactly n from nums. The remaining 2 * n elements will be divided into two equal parts:

  • The first n elements belonging to the first part and their sum is sumfirst.
  • The next n elements belonging to the second part and their sum is sumsecond.

The difference in sums of the two parts is denoted as sumfirst - sumsecond.

For example, if sumfirst = 3 and sumsecond = 2, their difference is 1. Similarly, if sumfirst = 2 and sumsecond = 3, their difference is -1.

Return the minimum difference possible between the sums of the two parts after the removal of n elements.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,1,4,2,5,6]
Output: -1
💡 Note: Remove [4,2,3] to get [1,5,6]. First part: [1], Second part: [5,6]. Difference: 1 - (5+6) = -10. This is one possible arrangement; optimal gives -1.
Example 2 — All Same Values
$ Input: nums = [7,7,7,7,7,7]
Output: 0
💡 Note: All elements equal, so any removal gives same sums for both parts. Difference is always 0.
Example 3 — Small Array
$ Input: nums = [20,40,20,70,30,50]
Output: -30
💡 Note: Remove [70,40,50] to get [20,20,30]. First: [20], Second: [20,30]. Difference: 20-50 = -30.

Constraints

  • nums.length == 3 * n
  • 1 ≤ n ≤ 105
  • 1 ≤ nums[i] ≤ 105

Visualization

Tap to expand
Minimum Difference in Sums After Removal INPUT nums array (3n = 6 elements, n = 2) 3 i=0 1 i=1 4 i=2 2 i=3 5 i=4 6 i=5 Remove n=2 elements Keep 2n=4 elements First n Middle Last n Goal: Minimize: sumfirst - sumsecond by choosing which n to remove Input Values: nums = [3,1,4,2,5,6] n = 2 ALGORITHM STEPS 1 Prefix Min Sum (Max Heap) Track min sum of n elements from left using max heap 2 Suffix Max Sum (Min Heap) Track max sum of n elements from right using min heap 3 Split Point Iteration Try each valid split point from index n-1 to 2n-1 4 Compute Minimum min(prefix[i] - suffix[i+1]) Example at split i=2: First part: [3,1,4] --> pick 2 Min sum = 1+3 = 4 Second part: [2,5,6] --> pick 2 Max sum = 5+6 = 11 But optimal: 1+4 - 5-6 = -6 Best found: 1+2 - 5-6 = -8? No FINAL RESULT Optimal Selection: 3 1 4 2 5 6 First part Removed Second Calculation: sumfirst = 3 + 1 = 4 sumsecond = 5 + 6 = 11 diff = 4 - 11 = -7 Actual Optimal: Keep [1,4] and [5,6] Remove [3,2] Output: -1 Key Insight: Use two heaps to efficiently track minimum sum for first part and maximum sum for second part. Max-heap maintains smallest n elements seen from left (for minimum first sum). Min-heap maintains largest n elements seen from right (for maximum second sum). Time: O(n log n) TutorialsPoint - Minimum Difference in Sums After Removal of Elements | Optimal Solution
Asked in
Google 15 Amazon 8 Microsoft 6
23.0K Views
Medium Frequency
~35 min Avg. Time
890 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