Magicsheet logo

Find the Smallest Divisor Given a Threshold

Medium
55.9%
Updated 6/1/2025

Find the Smallest Divisor Given a Threshold

1. What is this problem about?

The Find the Smallest Divisor Given a Threshold interview question is an optimization task based on division. You are given an array of integers and a threshold. You need to find the smallest positive integer divisor such that when every element in the array is divided by this divisor and the results are summed up, the total is less than or equal to the threshold. Each division result is rounded up to the nearest integer (ceiling).

2. Why is this asked in interviews?

Companies like Microsoft, PayPal, and Google ask the Find the Smallest Divisor coding problem to evaluate a candidate's ability to use Binary Search on the answer space. It tests if you can recognize a monotonic relationship: as the divisor increases, the sum of division results decreases. This insight allows you to find the optimal divisor efficiently in O(Nlog(maxA))O(N \log (\max A)) time.

3. Algorithmic pattern used

This problem follows the Binary Search on Value pattern.

  • Search Range: The smallest possible divisor is 1, and the largest necessary divisor is the maximum value in the array.
  • Feasibility Function: For a given mid, calculate the sum: sum(ceil(nums[i] / mid)).
  • Decision:
    • If the sum is \leq threshold, the current mid is a valid divisor, but we want the smallest one. Move the search to the left half: ans = mid, right = mid - 1.
    • If the sum is >> threshold, the divisor is too small. Move to the right half: left = mid + 1.

4. Example explanation

Array: [1, 2, 5, 9], Threshold: 6.

  • Try divisor = 4:
    • 1/4o1,2/4o1,5/4o2,9/4o31/4 o 1, 2/4 o 1, 5/4 o 2, 9/4 o 3. Sum = 1+1+2+3=71+1+2+3 = 7.
    • 7>67 > 6, so 4 is too small.
  • Try divisor = 5:
    • 1/5o1,2/5o1,5/5o1,9/5o21/5 o 1, 2/5 o 1, 5/5 o 1, 9/5 o 2. Sum = 1+1+1+2=51+1+1+2 = 5.
    • 565 \leq 6, so 5 is a candidate. Try smaller.
  • Try divisor = 4.5 (integer search will skip this). The smallest integer divisor is 5.

5. Common mistakes candidates make

  • Linear Search: Checking every divisor from 1 to max, which results in O(NmaxA)O(N \cdot \max A) and will time out.
  • Ceiling Division: Forgetting to round up or using floating point numbers which can lead to precision errors. The integer trick for ceil(a/b) is (a + b - 1) / b.
  • Incorrect Search Range: Starting the search range at 0, which leads to division-by-zero errors.

6. Interview preparation tip

Whenever you need to find the "minimum possible X" or "maximum possible X" that satisfies a condition, and the condition is monotonic, always try Binary Search on the answer. This is one of the most powerful and common Binary Search interview patterns.

Similar Questions