Find the Smallest Divisor Given a Threshold

Medium Binary Search Binary Search on Answer Space

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^4
  • 1 <= nums[i] <= 10^6
  • nums.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.

00:00
Testcase

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.

ApproachTimeSpace
Try every divisorO(n * max(nums))O(1)
Binary search on the divisorO(n log max(nums))O(1)

1Try every divisor

TimeO(n * max(nums))
SpaceO(1)

Test d = 1, 2, 3, ... and return the first one whose rounded-up quotient sum fits within the threshold.

  1. For d from 1 to max(nums): compute the sum of (x + d - 1) / d over nums.
  2. Return the first d whose sum is <= threshold.
  3. 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;
    }
}

2Binary search on the divisor

TimeO(n log max(nums))
SpaceO(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.

  1. lo = 1, hi = max(nums).
  2. While lo < hi: mid = lo + (hi - lo) / 2; if sum(ceil(x / mid)) <= threshold then hi = mid, else lo = mid + 1.
  3. Use a 64-bit sum (or stop early once it passes the threshold).
  4. 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;
    }
}

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.