Find the Smallest Divisor Given a Threshold
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Binary Search.
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
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Approach 1
Level I: Brute Force
Intuition
Try every divisor from 1 up to and check the sum condition.
⏱ $O(MaxValue \\cdot N)$💾 $O(1)$
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.
Approach 2
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.
⏱ $O((MaxValue - LowerBound) \\cdot N)$💾 $O(1)$
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.
Approach 3
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 .
⏱ $O(N \\log(MaxValue))$💾 $O(1)$
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.
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.