Given an array of integers nums and an integer threshold, we will choose a positive integer divisor, divide all the array by it, and sum the division's result. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.

Each result of the division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).

The test cases are generated so&nbsp;that there will be an answer.

&nbsp;
Example 1:

Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. 
If the divisor is 4 we can get a sum of 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). 


Example 2:

Input: nums = [44,22,33,11,1], threshold = 5
Output: 44


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 5 * 104
	1 &lt;= nums[i] &lt;= 106
	nums.length &lt;= threshold &lt;= 106


class Solution:
    def helper(self,nums,m):
        Sum = 0
        for n in nums:
            Sum += math.ceil(n/m)
        return Sum
    
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        l,r = 1, max(nums)
        while l < r:
            mid = (l+r)//2
            Sum = self.helper(nums,mid)
            if Sum > threshold:
                l = mid + 1
            else:
                r = mid     
        return r
class Solution {
    public int smallestDivisor(int[] a, int h) {
        int l = 1, r = a[0];
        for (int x : a) if (x > r) r = x;

        while (l < r) {
            int m = l + (r-l)/2;
            if (valid(a, m, h)) r = m;
            else l = m + 1;
        }

        return l;
    }

    private boolean valid(int[] a, int m, int h) {
        for (int x : a)
            if ((h -= (x + m-1)/m) < 0) return false;
        return true;
    }
}
class Solution {
public:
    int smallestDivisor(vector<int>& nums, int threshold) {
        int l=1,r=0;
        for(int n:nums)r=max(r,n);

        int mid,s=0;
        while(l<r){
            s=0;
            mid = l+(r-l)/2;
            for(int n:nums){
                s+= n/mid + (n%mid !=0);
            }
            if(s<= threshold){
                r=mid;
            }else{
                l=mid+1;
            }

        }
        return r;
    }
};
var smallestDivisor = function(nums, threshold) {
    let left = 1;
    let right = nums.reduce((r, x) => Math.max(r, x), 0);
    
    while (left <= right) {
        const div = Math.floor((left + right) / 2);
        const sum = nums.reduce((r, x) => r + Math.ceil(x / div), 0);
        
        if (sum <= threshold) {
            right = div - 1;
        } else {
            left = div + 1;
        }
    }
    
    return left;
};
Find the Smallest Divisor Given a Threshold