<p>Given an array of integers <code>nums</code> and an integer <code>threshold</code>, we will choose a positive integer <code>divisor</code>, divide all the array by it, and sum the division&#39;s result. Find the <strong>smallest</strong> <code>divisor</code> such that the result mentioned above is less than or equal to <code>threshold</code>.</p>

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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,5,9], threshold = 6
<strong>Output:</strong> 5
<strong>Explanation:</strong> 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). 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [44,22,33,11,1], threshold = 5
<strong>Output:</strong> 44
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>nums.length &lt;= threshold &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution {
public:
    int smallestDivisor(vector<int>& nums, int threshold) {
        sort(nums.begin(), nums.end());
        int start = 1, end = nums[nums.size()-1];
        int mid = (start + end )/2;
        int maxi = 0,mini = INT_MAX;
        int flag  = 0;
        while(start <= end){
            int sum = 0;
            for(int i=0; i<nums.size(); i++){
                if(nums[i] % mid == 0){
                    sum += nums[i] / mid;
                }
                else{
                    sum += (nums[i] / mid) + 1;
                }
            }
            if(sum > threshold){
                start = mid + 1;
                // mini = min(mini, mid);
            }
            else{
                end = mid - 1;
                // maxi = max(maxi, mid);
            }
            // else{
            //     mini = min(mini, mid);
            //     flag = 1;
            // }
            // cout << mid<< " ";
            // cout<<start<<" ";
            // cout<<end<<" ";
            mid = (start + end)/2;
        }
        // if(flag == 1){
        //     return mini;
        // }
        // if(maxi != 0)
        // return maxi;
        return start;
        // return mid;
    }
};
26000