class Solution:
    def minimizedMaximum(self, n: int, quantities: List[int]) -> int:
        def cond(m, n):
            return sum([(q // m) + (q % m > 0) for q in quantities]) <= n
        
        l, r = 1, max(quantities)
        while l < r:
            m = (l + r) // 2
            if cond(m, n):
                r = m
            else:
                l = m + 1
        return l
class Solution {
    public int minimizedMaximum(int n, int[] quantities) {
        
        int lo = 1;
        int hi = (int)1e5;
        
        int ans = -1;
        
        while(lo <= hi){
            
            int mid = (lo + hi)/2;
            
            if(isItPossible(mid, quantities, n)){
                ans = mid;
                hi = mid-1;
            }else{
                lo = mid+1;
            }
        }
        
        return ans;
    }
    
    private boolean isItPossible(int x, int[] quantities, int n){
        
        // isItPossible to distribute <= x products to each of the n shops
        for(int i=0; i<quantities.length; i++){
            
            int products = quantities[i];
            
            n -= Math.ceil(products/(x*1.0));
            
            if(n<0)     // means it requires more than n shops to distribute all products
                return false; 
        }
        
        return true; // distributed all products to exactly n shops
    }
}
Minimized Maximum of Products Distributed to Any Store
You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.

You need to distribute all products to the retail stores following these rules:


	A store can only be given at most one product type but can be given any amount of it.
	After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.


Return the minimum possible x.

&nbsp;
Example 1:

Input: n = 6, quantities = [11,6]
Output: 3
Explanation: One optimal way is:
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.


Example 2:

Input: n = 7, quantities = [15,10,10]
Output: 5
Explanation: One optimal way is:
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.


Example 3:

Input: n = 1, quantities = [100000]
Output: 100000
Explanation: The only optimal way is:
- The 100000 products of type 0 are distributed to the only store.
The maximum number of products given to any store is max(100000) = 100000.


&nbsp;
Constraints:


	m == quantities.length
	1 &lt;= m &lt;= n &lt;= 105
	1 &lt;= quantities[i] &lt;= 105


var minimizedMaximum = function(n, quantities) {
    const MAX = Number.MAX_SAFE_INTEGER;
    const m = quantities.length;

    let left = 1;
    let right = quantities.reduce((acc, num) => acc + num, 0);

    let minRes = MAX;

    while (left <= right) {
        const mid = (left + right) >> 1;

        if (canDistribute(mid)) {
            minRes = Math.min(minRes, mid);
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }

    return minRes;

    function canDistribute(minGiven) {
        const clonedQ = [...quantities];

        let j = 0;
        let i = 0;

        for (; i < n && j < m; ++i) {
            const remQ = clonedQ[j];

            if (remQ > minGiven) {
                clonedQ[j] -= minGiven;
            }
            else {
                ++j;
            }
        }

        return j === m;
    }
};
class Solution {
public:
  bool func(int N, int n, vector<int>& quantities){
    int temp = 0;
    for(int id = 0; temp <= n && id != quantities.size(); id++)
      temp += quantities[id] / N + (quantities[id] % N ? 1 : 0);
    
    return temp <= n;
  }
  
  int minimizedMaximum(int n, vector<int>& quantities) {
    int l = 1, r = *max_element(quantities.begin(), quantities.end());
    
    for(int m = (l + r)>>1; l <= r; m = (l + r)>>1) 
      func(m, n , quantities) ? r = m - 1 : l = m + 1; 
    
    return l;
  }
};