Minimize the Remaining Sum After Removing Up to K Consecutive Gems

Quick Overview

Remove one block of at most k consecutive gems from a row of positive values so the gems left have the smallest possible sum. It tests reasoning about contiguous removals under a length limit and turning that reasoning into an efficient linear scan.

Minimize the Remaining Sum After Removing Up to K Consecutive Gems

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an array of integers `gems` representing the values of gems in a row, and an integer `k` representing the maximum number of consecutive gems you can remove in one operation. You must perform the removal operation exactly once. The operation removes at most `k` consecutive gems from the row. Return the minimum possible sum of the remaining gems after the removal. ### Function Signature ```python def min_remaining_sum(gems: list[int], k: int) -> int: ``` ### Rules - The removed gems form one contiguous block of the original row, of length at most `k`. - If the removal takes every gem, the remaining sum is 0. ### Constraints - `1 <= len(gems) <= 1000` - `1 <= gems[i] <= 1000` - `1 <= k <= len(gems)` ### Examples ```text Input: gems = [3, 7, 2, 8, 1, 5], k = 2 Output: 16 ``` The total is 26. The best pair to remove is [3, 7] or [2, 8] (sum 10), so the remaining sum is 26 - 10 = 16. ```text Input: gems = [1, 2, 3, 4, 5], k = 3 Output: 3 ``` Removing [3, 4, 5] (sum 12) leaves [1, 2], so the minimum remaining sum is 3. ```text Input: gems = [2, 9, 4], k = 3 Output: 0 ``` Here `k` equals the length of the row, so one operation can remove all three gems and nothing is left.

Overview: Remove one block of at most k consecutive gems from a row of positive values so the gems left have the smallest possible sum. It tests reasoning about contiguous removals under a length limit and turning that reasoning into an efficient linear scan.

Uber logo
Uber
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of integers gems representing the values of gems in a row, and an integer k representing the maximum number of consecutive gems you can remove in one operation.

You must perform the removal operation exactly once. The operation removes at most k consecutive gems from the row.

Return the minimum possible sum of the remaining gems after the removal.

Function Signature

def min_remaining_sum(gems: list[int], k: int) -> int:

Rules

  • The removed gems form one contiguous block of the original row, of length at most k .
  • If the removal takes every gem, the remaining sum is 0.

Constraints

  • 1 <= len(gems) <= 1000
  • 1 <= gems[i] <= 1000
  • 1 <= k <= len(gems)

Examples

Input: gems = [3, 7, 2, 8, 1, 5], k = 2
Output: 16

The total is 26. The best pair to remove is [3, 7] or [2, 8] (sum 10), so the remaining sum is 26 - 10 = 16.

Input: gems = [1, 2, 3, 4, 5], k = 3
Output: 3

Removing [3, 4, 5] (sum 12) leaves [1, 2], so the minimum remaining sum is 3.

Input: gems = [2, 9, 4], k = 3
Output: 0

Here k equals the length of the row, so one operation can remove all three gems and nothing is left.