Find the Smallest Divisor Given a Threshold

Medium

Topics
ArrayBinary Search

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

10215293
The smallest divisor that satisfies it is 5.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,2,5,9], target = 6
Output: 5

Example 2

44022133211314
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

  1. Binary search d over [1, max(nums)].
  2. For each mid, compute the ceiling-division total; if it fits, search lower, else higher.
  3. while lo < hi with hi = mid converges 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 fail

Complexity

  • 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) // d is 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 < hi pairs with hi = mid. Using hi = mid - 1 can 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 lo
SensAI
Beta
Tap the mic to stop voice mode