Find the Smallest Divisor Given a Threshold
Find the Smallest Divisor Given a Threshold is a medium binary search problem solved with the binary search on answer space pattern.
The best approach, binary search on the divisor, runs in O(n log max(nums)) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from try every divisor up.
Problem
You get an array of positive integers nums and an integer threshold that is at least the length of nums. Pick a positive integer divisor d, divide every element by it rounding up, and add the results. Return the smallest d for which this sum is at most threshold.
Examples
Example 1
- Input
nums = [3, 8, 14], threshold = 7- Output
4- Why
- With 4 the rounded-up quotients are 1, 2, 4 (sum 7). With 3 they are 1, 3, 5 (sum 9), which is too large.
Example 2
- Input
nums = [10, 20, 30, 40], threshold = 4- Output
40- Why
- Each quotient is at least 1, so the sum can only reach 4 when every quotient is exactly 1.
Example 3
- Input
nums = [6, 2, 9], threshold = 20- Output
1- Why
- Dividing by 1 gives a sum of 17, already within the limit.
Constraints
1 <= nums.length <= 5 * 10^41 <= nums[i] <= 10^6nums.length <= threshold <= 10^6
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 50 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Try every divisor | O(n * max(nums)) | O(1) |
| Binary search on the divisor | O(n log max(nums)) | O(1) |
1Try every divisor
O(n * max(nums))O(1)Test d = 1, 2, 3, ... and return the first one whose rounded-up quotient sum fits within the threshold.
- For d from 1 to max(nums): compute the sum of (x + d - 1) / d over nums.
- Return the first d whose sum is <= threshold.
- d = max(nums) always works because every quotient is 1 and threshold >= n.
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int max = 0;
for (int x : nums) max = Math.max(max, x);
for (int d = 1; d < max; d++) {
long sum = 0;
for (int x : nums) {
sum += (x + d - 1) / d;
if (sum > threshold) break;
}
if (sum <= threshold) return d;
}
return max;
}
}class Solution:
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
mx = max(nums)
for d in range(1, mx):
total = 0
for x in nums:
total += (x + d - 1) // d
if total > threshold:
break
if total <= threshold:
return d
return mxclass Solution {
public:
int smallestDivisor(vector<int>& nums, int threshold) {
int mx = 0;
for (int x : nums) mx = max(mx, x);
for (int d = 1; d < mx; d++) {
long long sum = 0;
for (int x : nums) {
sum += (x + d - 1) / d;
if (sum > threshold) break;
}
if (sum <= threshold) return d;
}
return mx;
}
};function smallestDivisor(nums, threshold) {
let mx = 0;
for (const x of nums) mx = Math.max(mx, x);
for (let d = 1; d < mx; d++) {
let sum = 0;
for (const x of nums) {
sum += Math.floor((x + d - 1) / d);
if (sum > threshold) break;
}
if (sum <= threshold) return d;
}
return mx;
}2Binary search on the divisor
O(n log max(nums))O(1)Valid divisors form a suffix of [1, max(nums)] because the sum never increases as d grows. Binary search for the first valid one.
- lo = 1, hi = max(nums).
- While lo < hi: mid = lo + (hi - lo) / 2; if sum(ceil(x / mid)) <= threshold then hi = mid, else lo = mid + 1.
- Use a 64-bit sum (or stop early once it passes the threshold).
- Return lo.
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int lo = 1, hi = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (fits(nums, mid, threshold)) hi = mid;
else lo = mid + 1;
}
return lo;
}
private boolean fits(int[] nums, int d, int threshold) {
long sum = 0;
for (int x : nums) {
sum += (x + d - 1) / d;
if (sum > threshold) return false;
}
return true;
}
}class Solution:
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
def fits(d: int) -> bool:
total = 0
for x in nums:
total += (x + d - 1) // d
if total > threshold:
return False
return True
lo, hi = 1, max(nums)
while lo < hi:
mid = (lo + hi) // 2
if fits(mid):
hi = mid
else:
lo = mid + 1
return loclass Solution {
public:
int smallestDivisor(vector<int>& nums, int threshold) {
int lo = 1, hi = 0;
for (int x : nums) hi = max(hi, x);
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (fits(nums, mid, threshold)) hi = mid;
else lo = mid + 1;
}
return lo;
}
private:
bool fits(vector<int>& nums, int d, int threshold) {
long long sum = 0;
for (int x : nums) {
sum += (x + d - 1) / d;
if (sum > threshold) return false;
}
return true;
}
};function smallestDivisor(nums, threshold) {
const fits = (d) => {
let sum = 0;
for (const x of nums) {
sum += Math.floor((x + d - 1) / d);
if (sum > threshold) return false;
}
return true;
};
let lo = 1, hi = 0;
for (const x of nums) hi = Math.max(hi, x);
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (fits(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}Edge cases to test
- threshold equals nums.length (answer is max(nums))
- threshold large enough that the answer is 1
- Single element
- Sum with divisor 1 does not fit in a 32-bit int
Hints
Hint 1
The sum only goes down (or stays the same) as the divisor grows. What does that tell you about the set of valid divisors?
Hint 2
The answer always lies between 1 and max(nums). Binary search on it.
Hint 3
ceil(a / d) can be computed as (a + d - 1) / d with integers.
FAQ
What is the best time complexity for Find the Smallest Divisor Given a Threshold?
Binary search on the divisor runs in O(n log max(nums)) time and O(1) extra space.
Which pattern does Find the Smallest Divisor Given a Threshold use?
It is a binary search problem that uses the binary search on answer space pattern. Other problems with the same pattern: Koko Eating Bananas, Minimum Number of Days to Make m Bouquets, Separate Squares I.
Is there a brute force solution for Find the Smallest Divisor Given a Threshold?
Yes. Try every divisor takes O(n * max(nums)) time and O(1) space. Test d = 1, 2, 3, ...
Which edge cases should I test for Find the Smallest Divisor Given a Threshold?
threshold equals nums.length (answer is max(nums)); threshold large enough that the answer is 1; Single element; Sum with divisor 1 does not fit in a 32-bit int.