Find the Smallest Divisor Given a Threshold

Pattern: Binary search on the answer

medium

Bigger divisors give smaller rounded-up sums. So binary search the divisor from 1 to the largest value, and keep the smallest one whose sum stays within the threshold.

The problem

You are given an array of positive whole numbers and a threshold. Choose a positive divisor, divide every number by it, round each result up, and add them. Return the smallest divisor whose sum is at most the threshold. For example, [1, 2, 5, 9] with threshold 6 gives 5, and [44, 22, 33, 11, 1] with threshold 5 gives 44.

The function name smallestDivisor matches LeetCode. Use the link above for the exact statement and limits. An answer always exists, because the threshold is at least the array's length.

Think first

  • When the divisor grows, can the sum grow?
  • Which divisor surely works, and which is the smallest you would ever try?
  • How do you divide and round up with whole numbers only?

Hints

    Brute force, and why it fails

    Try d = 1, 2, 3, … and stop at the first that fits: O(max × n). Binary search the divisor: O(n log max).

    The key insight

    The answer is a number in a known range, and checking a guess is one pass. Because the check only gets easier as the guess grows, the smallest passing guess can be found by halving.

    Dry run

    10 
    21lo
    52 
    93 

    nums = [1, 2, 5, 9] and threshold = 6. Divisors from 1 to 9 are possible, since 9 always gives a sum of 4.

    Step 1 of 4

    The plan

    lo = 1, hi = max(nums)
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if sum of ceil(v / mid) over nums <= threshold: hi = mid
        else: lo = mid + 1
    return lo
    

    Complexity

    Time
    O(n log max)
    Space
    O(1)

    About log₂ max guesses, each checked in O(n).

    Edge cases

    • One value: the smallest d with ceil(v / d) ≤ threshold.
    • Threshold equal to n: the answer is the largest value.
    • All ones: the answer is 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int smallestDivisor(int[] nums, int threshold) {
            int lo = 1, hi = 0;
            for (int v : nums) hi = Math.max(hi, v);    // dividing by the largest value gives n, which always fits
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (total(nums, mid) <= threshold) hi = mid;   // mid works: try smaller
                else lo = mid + 1;                             // too big a sum: need a larger divisor
            }
            return lo;
        }
    
        private long total(int[] nums, int d) {
            long sum = 0;
            for (int v : nums) sum += (v + d - 1) / d;  // each division rounded up
            return sum;
        }
    }

    C++

    class Solution {
    public:
        int smallestDivisor(vector<int>& nums, int threshold) {
            int lo = 1, hi = *max_element(nums.begin(), nums.end());   // the largest value always works
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (total(nums, mid) <= threshold) hi = mid;   // mid works: try smaller
                else lo = mid + 1;                             // too big a sum: need a larger divisor
            }
            return lo;
        }
    
        long long total(vector<int>& nums, int d) {
            long long sum = 0;
            for (int v : nums) sum += (v + d - 1) / d;  // each division rounded up
            return sum;
        }
    };

    Python

    class Solution:
        def smallestDivisor(self, nums: list[int], threshold: int) -> int:
            lo, hi = 1, max(nums)                   # the largest value always works
            while lo < hi:
                mid = (lo + hi) // 2
                if sum((v + mid - 1) // mid for v in nums) <= threshold:
                    hi = mid                        # mid works: try smaller
                else:
                    lo = mid + 1                    # too big a sum: need a larger divisor
            return lo

    Common mistakes

    • Using plain division without rounding up.
    • Starting lo at 0, which divides by zero.
    • Adding quotients in a 32-bit total when n and the values are large.

    Similar problems