Find the Smallest Divisor Given a Threshold
Master this topic with zero to advance depth.
Find the Smallest Divisor Given a Threshold
Given an array nums and an integer threshold, find the smallest divisor such that the sum of the division results (rounded up) is threshold.
Examples
Level I: Brute Force
Intuition
Try every divisor from 1 up to and check the sum condition.
Detailed Dry Run
nums=[1,2,5,9], threshold=6.
- d=4: ceil(1/4)+ceil(2/4)+ceil(5/4)+ceil(9/4) = 1+1+2+3 = 7 > 6.
- d=5: ceil(1/5)+ceil(2/5)+ceil(5/5)+ceil(9/5) = 1+1+1+2 = 5 <= 6. Return 5.
Level II: Optimal (Mathematical Bound Optimization)
Intuition
Instead of starting the brute force from , we can observe that must be at least . This provides a tighter lower bound for our search.
Detailed Dry Run
nums=[1,2,5,9], threshold=6. Sum=17. Lower bound .
- d=3: sum = 7 > 6.
- d=4: sum = 7 > 6.
- d=5: sum = 5 <= 6. Return 5.
Level III: Optimal (Binary Search on Answer)
Intuition
The sum of division results is a non-increasing function of the divisor . Binary search in the range .
Detailed Dry Run
nums=[1,2,5,9], threshold=6. [1, 9].
- Mid 5: sum = 5 <= 6. R=5.
- Mid 2: sum = 1+1+3+5 = 10 > 6. L=3.
- Mid 3: sum = 1+1+2+3 = 7 > 6. L=4.
- Mid 4: sum = 1+1+2+3 = 7 > 6. L=5. Result 5.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.