Tensorum
LeetCode 1283 · Binary Search on Answer

Find the Smallest Divisor Given a Threshold

LeetCode 1283 · Medium · solution, intuition & step-by-step walkthrough

Monotonic total ⇒ binary search on d.

MediumArrayBinary Search
128 msruntime · beats 70%
22.7 MBmemory · beats 100%
1approach
1accepted runs · last 25 Apr 2025

This is how I solved Find the Smallest Divisor Given a Threshold (LeetCode 1283), a medium problem from the Binary Search on Answer pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

A bigger divisor makes every ceil(num / d) smaller, so the total only goes down as d grows. Binary search the smallest divisor whose total is within the threshold.

Approach

  1. 1

    Search d in [1, max(nums)].

  2. 2

    total(d) = Σ ceil(num / d).

  3. 3

    Within threshold → try smaller d; else larger.

How I solved it

Binary search on divisorPythonO(n log M) time
class Solution:
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        def isTotalSumWithinThreshold(mid):
            totalSum=0
            for num in nums:
                totalSum+=math.ceil(num/mid)
            if totalSum<=threshold:
                return True
            else:
                False
        low =1
        high = max(nums)
        while low<=high:
            mid = (low+high)//2
            withinThreshold = isTotalSumWithinThreshold(mid)
            if withinThreshold:
                high = mid-1
            else:
                low=mid+1
        return low
Loading the step-through…

Find the Smallest Divisor Given a Threshold — FAQ

What is the intuition behind Find the Smallest Divisor Given a Threshold?

A bigger divisor makes every ceil(num / d) smaller, so the total only goes down as d grows. Binary search the smallest divisor whose total is within the threshold.

How do you solve Find the Smallest Divisor Given a Threshold (LeetCode 1283)?

1. Search d in [1, max(nums)]. 2. total(d) = Σ ceil(num / d). 3. Within threshold → try smaller d; else larger.

What is the time and space complexity of Find the Smallest Divisor Given a Threshold?

Time: O(n log M). Space: O(1). (Accepted approach: Binary search on divisor.)

What are common mistakes in Find the Smallest Divisor Given a Threshold?

The helper falls through to None when the total is too big — None is falsy, so it still works, but return False explicitly.