Find the Smallest Divisor Given a Threshold

Given an array of integers nums and an integer threshold, choose a positive integer divisor, divide every element of nums by it, round each quotient up to the nearest integer, and add the results. Return the smallest divisor for which this sum is less than or equal to threshold. The tests guarantee that an answer exists.

30:00

Find the Smallest Divisor Given a Threshold
medium
Free written solution
Topics
Companies

Given an array of integers nums and an integer threshold, choose a positive integer divisor, divide every element of nums by it, round each quotient up to the nearest integer, and add the results. Return the smallest divisor for which this sum is less than or equal to threshold. The tests guarantee that an answer exists.

Example 1:
Input: {"nums":[1,2,5,9],"threshold":6}
Output: 5
Constraints:
  • 1≤nums.length≤5×1041 \leq \text{nums.length} \leq 5 \times 10^4

  • 1≤nums[i]≤1061 \leq \text{nums}[i] \leq 10^6

  • nums.length≤threshold≤106\text{nums.length} \leq \text{threshold} \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,5,9],"threshold":6}

Dividing by d cuts each number into ceil(x / d) pieces. A bigger d means fewer pieces, so the piece count only falls as d grows.

Divisor d on 1 .. 9

lo=1
hi=9

● sum fits

● sum too big

band = [lo, hi]

Each number as a bar

1
?
2
?
5
?
9
?

Pieces vs threshold

sum = - / 6

Variables
VariableValue
threshold6
DepthFunction Call
Stack empty
0/24