package leetcode;

/**
 * https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/
 */
public class Problem1283 {
    public int smallestDivisor(int[] nums, int threshold) {
        int answer = 0;
        for (int i = 1; i <= 1000000; i++) {
            int sum = 0;
            for (int num : nums) {
                sum += -Math.floorDiv(-num, i); // opposite of ceilDiv
            }
            if (sum <= threshold) {
                answer = i;
                break;
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1283 prob = new Problem1283();
        System.out.println(prob.smallestDivisor(new int[]{1, 2, 5, 9}, 6)); // 5
        System.out.println(prob.smallestDivisor(new int[]{2, 3, 5, 7, 11}, 11)); // 3
        System.out.println(prob.smallestDivisor(new int[]{19}, 5)); // 4
    }
}

@@ -221,44 +221,164 @@
     
-for (int i = 1; i %3C= 1000000; i++) %7B
+int left = 1;%0A        int right = 1000000;%0A        int mid = 0;%0A        int maxSum = 0;%0A        while (left %3C right) %7B%0A            mid = (left + right) / 2;
 %0A   
@@ -478,17 +478,19 @@
 v(-num, 
-i
+mid
 ); // op
@@ -545,10 +545,9 @@
 sum 
-%3C=
+%3E
  thr
@@ -576,56 +576,109 @@
     
-answer = i;%0A                break;%0A            %7D
+left = mid + 1;%0A            %7D else %7B%0A                right = mid;%0A                if (sum %3E maxSum) %7B
 %0A   
@@ -682,26 +682,24 @@
         
-%7D%0A
         
 return a
@@ -694,360 +694,221 @@
     
-return
+   
  answer
+ = mid
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1283 prob = new Problem1283();%0A        System.out.println(prob.smallestDivisor(new int%5B%5D%7B1, 2, 5, 9%7D, 6)); // 5%0A        System.out.println(prob.smallestDivisor(new int%5B%5D%7B2, 3, 5, 7, 11%7D, 11)); // 3%0A        System.out.println(prob.smallestDivisor(new int%5B%5D%7B19%7D, 5)); // 4
+            maxSum = sum;%0A                %7D else if (sum == maxSum) %7B%0A                    answer = Math.min(answer, mid);%0A                %7D%0A            %7D%0A%0A        %7D%0A        return answer;
 %0A   

Solve problem 1283