Find the Smallest Divisor Given a Threshold
Medium
Topics
Given an array nums and an integer target (the threshold), find the smallest positive divisor d such that the sum of ceil(nums[i] / d) over all elements is less than or equal to target. It is guaranteed that a valid divisor exists.
Example 1
Input: nums = [1,2,5,9], target = 6 Output: 5
Example 2
Input: nums = [44,22,33,11,1], target = 5 Output: 44
Constraints
- 1 <= nums.length <= 5*10^4
- 1 <= nums[i] <= 10^6
- nums.length <= target <= 10^6
Editorial — intuition, approach, complexity and pitfalls
Intuition
Each candidate divisor d gives a total of sum(ceil(nums[i] / d)), and we want the smallest d whose total fits the threshold. Trying every divisor from 1 upward is O(n * max) and too slow.
The total is monotonically non-increasing in d: a larger divisor can only shrink each quotient. So the predicate "total fits the threshold" is false for small divisors and true from some point onward — the single flip that binary search needs.
Bounds: 1 is the smallest meaningful divisor, and max(nums) is enough at the top, since any divisor that large makes every quotient 1 and the total equal to n, which the constraints guarantee is within the threshold.
💡 ceil(x / d) without floating point is (x + d - 1) // d. Using math.ceil on a float division invites precision error on large values, and this is the idiom that avoids it.
Approach
- Binary search
dover[1, max(nums)]. - For each
mid, compute the ceiling-division total; if it fits, search lower, else higher. while lo < hiwithhi = midconverges on the smallest feasible divisor.
Dry Run
nums = [1,2,5,9], threshold = 6
lo=1 hi=9
mid=5 ceil: 1+1+1+2 = 5 <= 6 feasible, hi=5
mid=3 ceil: 1+1+2+3 = 7 > 6 too small, lo=4
mid=4 ceil: 1+1+2+3 = 7 > 6 too small, lo=5
lo == hi == 5
answer 5
check d=4: ceil(9/4) = 3, total 7 > 6, so 4 really does failComplexity
- Time — O(n log(max)): each probe is one linear pass, over a range bounded by the largest value — about 20 probes at the constraint limit.
- Space — O(1).
Common Pitfalls
- Using floor division instead of ceiling. The problem rounds each quotient up, and floor division silently makes small divisors look feasible.
- Computing the ceiling with floats.
(x + d - 1) // dis exact;math.ceil(x / d)can be off by one on large values. - Starting the search at 0, which divides by zero on the first probe.
- Mixing loop shapes:
while lo < hipairs withhi = mid. Usinghi = mid - 1can step past the answer. - Searching the threshold rather than the divisor. The threshold is the constraint; the divisor is the unknown.
Full Solution — all languages
class Solution:
def smallestDivisor(self, nums, target):
def total(d):
s = 0
for x in nums:
s += (x + d - 1) // d
return s
lo, hi = 1, max(nums)
while lo < hi:
mid = (lo + hi) // 2
if total(mid) <= target:
hi = mid
else:
lo = mid + 1
return loimport java.util.*;
class Solution {
public int smallestDivisor(int[] nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo < hi) {
int mid = (lo + hi) / 2;
if (total(nums, mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
int total(int[] nums, int d) {
int s = 0;
for (int x : nums) s += (x + d - 1) / d;
return s;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int smallestDivisor(vector<int>& nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = max(hi, x);
auto total = [&](int d) {
int s = 0;
for (int x : nums) s += (x + d - 1) / d;
return s;
};
while (lo < hi) {
int mid = (lo + hi) / 2;
if (total(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var smallestDivisor = function(nums, target) {
let lo = 1, hi = 0;
for (const x of nums) hi = Math.max(hi, x);
const total = (d) => {
let s = 0;
for (const x of nums) s += Math.floor((x + d - 1) / d);
return s;
};
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (total(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
};