2163. Minimum Difference in Sums After Removal of Elements
Description
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 issumfirst
. - The next
n
elements belonging to the second part and their sum issumsecond
.
The difference in sums of the two parts is denoted as sumfirst - sumsecond
.
- For example, if
sumfirst = 3
andsumsecond = 2
, their difference is1
. - Similarly, if
sumfirst = 2
andsumsecond = 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 * n
1 <= n <= 105
1 <= nums[i] <= 105
Solutions
Solution 1: Priority Queue (Max and Min Heap) + Prefix and Suffix Sum + Enumeration of Split Points
The problem is essentially equivalent to finding a split point in \(nums\), dividing the array into two parts. In the first part, select the smallest \(n\) elements, and in the second part, select the largest \(n\) elements, so that the difference between the sums of the two parts is minimized.
We can use a max heap to maintain the smallest \(n\) elements in the prefix, and a min heap to maintain the largest \(n\) elements in the suffix. We define \(pre[i]\) as the sum of the smallest \(n\) elements among the first \(i\) elements of the array \(nums\), and \(suf[i]\) as the sum of the largest \(n\) elements from the \(i\)-th element to the last element of the array. During the process of maintaining the max and min heaps, update the values of \(pre[i]\) and \(suf[i]\).
Finally, we enumerate the split points in the range of \(i \in [n, 2n]\), calculate the value of \(pre[i] - suf[i + 1]\), and take the minimum value.
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array \(nums\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|