Minimum Difference in Sums After Removal of Elements Problem
Description
LeetCode Problem 2163.
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
nelements belonging to the first part and their sum issum_first. - The next
nelements belonging to the second part and their sum issum_second.
The difference in sums of the two parts is denoted as sum_first - sum_second.
- For example, if
sum_first = 3andsum_second = 2, their difference is1. - Similarly, if
sum_first = 2andsum_second = 3, their difference is-1.
Return the minimum difference possible between the sums of the two parts after the removal of n elements.
Example 1:
Input: nums = [3,1,2]
Output: -1
Explanation: Here, nums has 3 elements, so n = 1.
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
Example 2:
Input: nums = [7,9,5,8,1,3]
Output: 1
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.
Constraints:
nums.length == 3 * n1 <= n <= 10^51 <= nums[i] <= 10^5
Solution
Approach: Two heaps with prefix and suffix sums
Removing n elements and splitting the rest into two halves is the same as choosing a split point in nums: from the part on the left we keep the n smallest elements, and from the part on the right we keep the n largest, which makes sum_first - sum_second as small as possible.
Let pre[i] be the sum of the n smallest elements among the first i elements. Scan from the left with a max heap holding the n smallest values seen so far: push each element, and when the heap grows past n, pop the largest. Likewise, let suf[i] be the sum of the n largest elements among nums[i-1..], maintained with a min heap while scanning from the right.
The split point i ranges over [n, 2n]: the left part is the first i elements and the right part is the rest. The answer is the minimum of pre[i] - suf[i + 1].
Complexity: time O(n log n), space O(n), where n is the length of nums.
import heapq
class Solution:
def minimumDifference(self, nums: List[int]) -> int:
m = len(nums)
n = m // 3
s = 0
pre = [0] * (m + 1)
q1 = []
for i, x in enumerate(nums[: n * 2], 1):
s += x
heapq.heappush(q1, -x)
if len(q1) > n:
s -= -heapq.heappop(q1)
pre[i] = s
s = 0
suf = [0] * (m + 1)
q2 = []
for i in range(m, n, -1):
x = nums[i - 1]
s += x
heapq.heappush(q2, x)
if len(q2) > n:
s -= heapq.heappop(q2)
suf[i] = s
return min(pre[i] - suf[i + 1] for i in range(n, n * 2 + 1))