Minimize the largest bag after at most a given number of splits

Quick Overview

Given the number of balls in each bag and a cap on how many times you may split one bag into two non-empty bags, find the smallest possible size of the largest bag. It tests searching over the answer space, proving a feasibility check is monotone, and getting integer ceiling arithmetic right.

Minimize the largest bag after at most a given number of splits

Company: AT&T

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You have `n` bags, and bag `i` holds `nums[i]` balls. You may perform the following operation at most `max_operations` times: pick any bag and split it into two new bags, each holding a positive number of balls. For example, a bag of 5 balls can become bags of 1 and 4 balls, or bags of 2 and 3 balls. Your penalty is the largest number of balls in any single bag after you finish. Return the minimum possible penalty. ### Function Signature ```python def minimum_size(nums: list[int], max_operations: int) -> int: ``` ### Rules - A split of a bag holding `x` balls produces two bags holding `a` and `x - a` balls, where `1 <= a <= x - 1`. - Any bag can be split, including a bag created by an earlier split. - You may use fewer than `max_operations` operations, including none. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= max_operations <= 10^9` - `1 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [9], max_operations = 2 Output: 3 ``` Split 9 into 6 and 3, then split 6 into 3 and 3, which leaves `[3, 3, 3]`. A penalty of 2 would need at least five bags, and that takes four splits. ```text Input: nums = [2, 4, 8, 2], max_operations = 4 Output: 2 ``` Split the 8 into 4 and 4, split each of those two 4s into 2 and 2, then split the original bag of 4 into 2 and 2. That uses four operations and every bag holds 2 balls. A penalty of 1 would need 1 + 3 + 7 + 1 = 12 splits. ```text Input: nums = [7, 17], max_operations = 2 Output: 7 ``` Two splits can turn 17 into bags of 6, 6 and 5, and then the untouched bag of 7 is the largest. A penalty of 6 would need one split for the 7 and two for the 17, three in total.

Overview: Given the number of balls in each bag and a cap on how many times you may split one bag into two non-empty bags, find the smallest possible size of the largest bag. It tests searching over the answer space, proving a feasibility check is monotone, and getting integer ceiling arithmetic right.

AT&T logo
AT&T
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You have n bags, and bag i holds nums[i] balls. You may perform the following operation at most max_operations times: pick any bag and split it into two new bags, each holding a positive number of balls. For example, a bag of 5 balls can become bags of 1 and 4 balls, or bags of 2 and 3 balls.

Your penalty is the largest number of balls in any single bag after you finish. Return the minimum possible penalty.

Function Signature

def minimum_size(nums: list[int], max_operations: int) -> int:

Rules

  • A split of a bag holding x balls produces two bags holding a and x - a balls, where 1 <= a <= x - 1 .
  • Any bag can be split, including a bag created by an earlier split.
  • You may use fewer than max_operations operations, including none.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= max_operations <= 10^9
  • 1 <= nums[i] <= 10^9

Examples

Input: nums = [9], max_operations = 2
Output: 3

Split 9 into 6 and 3, then split 6 into 3 and 3, which leaves [3, 3, 3]. A penalty of 2 would need at least five bags, and that takes four splits.

Input: nums = [2, 4, 8, 2], max_operations = 4
Output: 2

Split the 8 into 4 and 4, split each of those two 4s into 2 and 2, then split the original bag of 4 into 2 and 2. That uses four operations and every bag holds 2 balls. A penalty of 1 would need 1 + 3 + 7 + 1 = 12 splits.

Input: nums = [7, 17], max_operations = 2
Output: 7

Two splits can turn 17 into bags of 6, 6 and 5, and then the untouched bag of 7 is the largest. A penalty of 6 would need one split for the 7 and two for the 17, three in total.