↓Skip to main content
  1. LeetCode/

LeetCode 2163: Minimum Difference in Sums After Removal of Elements

·2 mins· ·
LeetCode Hard Dynamic-Programming Heap Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-09-05

  • Submissions: Accepted (first try)

Approach
#

After removing n elements, the first n remaining form part one and the last n part two. Equivalently, choose a boundary k (n <= k <= 2n): part one takes n elements from nums[:k] and part two n from nums[k:].

To minimize sum_first - sum_second:

  • Part one takes the n smallest of nums[:k]: scan left to right with a max-heap of the current smallest n, dropping the largest when there are too many. left_min[k - 1] = the sum of the smallest n among the first k.
  • Part two takes the n largest of nums[k:]: scan right to left with a min-heap, dropping the smallest. right_max[k] = the sum of the largest n from k on.

The answer is min(left_min[k - 1] - right_max[k]).

My Solution
#

import heapq

class Solution:
    def minimumDifference(self, nums: List[int]) -> int:
        n = len(nums) // 3

        left_min = [0] * (3 * n)                   # left_min[i]: sum of the n smallest in nums[:i+1]
        heap, total = [], 0                        # max-heap (negated values)
        for i in range(2 * n):
            heapq.heappush(heap, -nums[i])
            total += nums[i]
            if len(heap) > n:
                total += heapq.heappop(heap)       # drop the largest (stored negated)
            left_min[i] = total

        right_max = [0] * (3 * n)                  # right_max[i]: sum of the n largest in nums[i:]
        heap, total = [], 0                        # min-heap
        for i in range(3 * n - 1, n - 1, -1):
            heapq.heappush(heap, nums[i])
            total += nums[i]
            if len(heap) > n:
                total -= heapq.heappop(heap)       # drop the smallest
            right_max[i] = total

        return min(left_min[k - 1] - right_max[k] for k in range(n, 2 * n + 1))

Complexity
#

Time O(n log n), space O(n).

Mistakes to Avoid
#

  • The boundary runs from n to 2n: both sides need at least n elements to choose from.
  • Python only has a min-heap; a max-heap stores negated values, and popped values are negated too.

Interview Explanation
#

Removing n elements amounts to choosing a boundary, with part one taking n from the left and part two n from the right. To minimize the difference, the left takes its n smallest and the right its n largest. A left-to-right max-heap gives each prefix’s smallest-n sum, a right-to-left min-heap each suffix’s largest-n sum, and I try every boundary. Time O(n log n).

Related

LeetCode 4041: Minimum Operations to Form Subset Sum II
·2 mins
LeetCode Hard Dynamic-Programming Knapsack Breadth-First-Search Array
LeetCode 4041 note: with operations in any order, BFS finds every value a number can become (up to sum) with the fewest steps, then the same grouped 0/1 knapsack as 4040
LeetCode 2585: Number of Ways to Earn Points
·2 mins
LeetCode Hard Dynamic-Programming Knapsack Array
LeetCode 2585 note: a bounded (grouped) knapsack; each question type takes 0 to count questions, with capacity running backwards and the count enumerated inside
LeetCode 410: Split Array Largest Sum
·2 mins
LeetCode Hard Dynamic-Programming Binary-Search Greedy Prefix-Sum Array
LeetCode 410 note: binary search the answer; for a cap mid, greedily fill each segment to count the fewest segments, and mid works if that count is <= k
LeetCode 2355: Maximum Number of Books You Can Take
·2 mins
LeetCode Hard Dynamic-Programming Monotonic-Stack Stack Array
LeetCode 2355 note: ending at i, each shelf to the left takes one fewer book until a bottleneck shelf j with too few books; a monotonic stack finds j, and dp[i] = dp[j] + an arithmetic series
LeetCode 3989: Maximum Consistent Columns in a Grid
·2 mins
LeetCode Hard Dynamic-Programming Matrix Array
LeetCode 3989 note: the kept columns form a subsequence constrained only between neighbors; an LIS-style DP with dp[j] = the longest valid sequence ending at column j
LeetCode 1402: Reducing Dishes
·2 mins
LeetCode Hard Dynamic-Programming Greedy Sorting Array
LeetCode 1402 note: sort and take dishes from most satisfying down; each new dish goes first and shifts the chosen ones one slot later, adding the current chosen sum, so keep going while that sum is positive