You are given an integer array nums where the ith 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&nbsp;after performing the operations.

&nbsp;
Example 1:

Input: nums = [9], maxOperations = 2
Output: 3
Explanation: 
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -&gt; [6,3].
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -&gt; [3,3,3].
The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.


Example 2:

Input: nums = [2,4,8,2], maxOperations = 4
Output: 2
Explanation:
- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -&gt; [2,4,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,4,4,4,2] -&gt; [2,2,2,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,4,4,2] -&gt; [2,2,2,2,2,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,4,2] -&gt; [2,2,2,2,2,2,2,2].
The bag with the most number of balls has 2 balls, so your penalty is 2 an you should return 2.


Example 3:

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


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= maxOperations, nums[i] &lt;= 109


class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        l, r = 1, max(nums)
        while l < r:
            mid = (l + r) // 2
            if sum([(n - 1) // mid for n in nums]) > maxOperations: 
                l = mid + 1
            else:
                r = mid
        return l
class Solution {
    public int minimumSize(int[] nums, int maxOperations) {
	//initiate the boundary for possible answers, here if you let min=1 it will still work for most cases except for some corner cases. We make max=100000000 because nums[i] <= 10^9. You can choose to sort the array and make the max= arr.max, at the price of time consumption.
	//The answer should be the minimized max value.
        int min = 0;
        int max = 1000000000;
		//Compared with min<max or min <= max, min + 1 < max will avoid infinite loops e.g. when min = 2, max = 3
        while (min +1< max) {
            int mid = (max - min)/2 + min;
			//count indicates the operation times with atmost mid balls in bag.
            int count = 0;
            for (int a: nums) {
			//this is the same as Math. ceil(a/mid) - 1=> math.ceil(a/mid) gives the number of divided bags, we subtract the number by 1 to get the subdivision operation times.
                count+=(a-1)/mid;
            }
			//if count < maxOperations, max WOULD be further minimized and set to mid; 
			//if count = maxOperations, max still COULD be further minimized and set to mid. 
			//so we combine < and = cases together in one if condition
            if (count <= maxOperations) {
			//max = mid - 1 will not work in this case becasue mid could be the correct answer. 
			//To not miss the correct answer we set a relatively "loose" boundary for max and min.
                max = mid;
            } else{
                min = mid;
            }
        }
		//Now we find the minimized max value
        return max;
    }
}
class Solution {
public:
    bool isPossible(vector<int>&nums,int mid_penalty,int maxOperations)
    {
        int operations=0;

        for(int i=0;i<nums.size();i++)
        {
            operations+=(nums[i]/mid_penalty); //Operations Nedded to divide that element.
            if(nums[i]%mid_penalty==0) //if it is completely divisible means less 1 less is needed for that nums.
                operations--;
        }

        return operations<=maxOperations?1:0; //If operations are less than maxOperations it is one of our ans.
    }
    int minimumSize(vector<int>& nums, int maxOperations) {
        int low_penalty=1,high_penalty=*max_element(nums.begin(),nums.end());
        int ans=high_penalty;
        while(low_penalty<=high_penalty)
        {
            int mid_penalty=low_penalty+(high_penalty-low_penalty)/2;
            if(mid_penalty==0) //To avoid divison by zero.
                break;
            if(isPossible(nums,mid_penalty,maxOperations))
            {
                ans=mid_penalty;
                high_penalty=mid_penalty-1;
            }
            else
                low_penalty=mid_penalty+1;
        }
        return ans;
    }
};
var minimumSize = function(nums, maxOperations) {
  let i = 1
  let j = Math.max(...nums)

  while (i <= j) {
    let mid = Math.floor((j-i)/2 + i)
    let count = 0
    nums.forEach(n => count += Math.floor((n-1)/mid))

    if (count <= maxOperations) {
      j = mid - 1
    } else {
      i = mid + 1
    }
  }
  return i
};
Minimum Limit of Balls in a Bag