Minimum Limit of Balls in a Bag

You are given an integer array nums where the i^th bag contains nums[i] balls. You are also given an integer maxOperations.

You can perform the following operation at most maxOperations times:

  • Take any bag of balls and divide it into two new bags with a positive number of balls.
  • For example, a bag of 5 balls can become two new bags of 1 and 4 balls, or two new bags of 2 and 3 balls.

Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.

Return the minimum possible penalty after performing the operations.

Example 1
Inputnums = [9], maxOperations = 2
Output3
After splitting 9 into 6 and 3, then splitting 6 into 3 and 3, the maximum number of balls in any bag is 3.
Example 2
Inputnums = [2,4,8,2], maxOperations = 4
Output2
By splitting the bag with 8 into two 4s and then splitting each 4 into two 2s, every bag has at most 2 balls.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= maxOperations, nums[i] <= 10^9

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate