Find the Smallest Divisor Given a Threshold - Problem
Find the Smallest Divisor Given a Threshold

You're given an array of integers nums and an integer threshold. Your task is to find the smallest positive divisor such that when you divide each element in the array by this divisor and sum up all the results, the total is less than or equal to the threshold.

Here's the catch: each division result must be rounded up to the nearest integer (ceiling function). For example:
7 ÷ 3 = 2.33... rounds up to 3
10 ÷ 2 = 5.0 stays 5
1 ÷ 4 = 0.25 rounds up to 1

Goal: Find the minimum divisor that keeps the sum within the threshold limit. The problem guarantees a solution exists.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 2, 5, 9], threshold = 6
Output: 5
💡 Note: With divisor 5: ceil(1/5) + ceil(2/5) + ceil(5/5) + ceil(9/5) = 1 + 1 + 1 + 2 = 5 ≤ 6. Smaller divisors like 4 give sum = 6, but we want the smallest, so 5 is correct.
example_2.py — Edge Case
$ Input: nums = [44, 22, 33, 11, 1], threshold = 5
Output: 44
💡 Note: With divisor 44: ceil(44/44) + ceil(22/44) + ceil(33/44) + ceil(11/44) + ceil(1/44) = 1 + 1 + 1 + 1 + 1 = 5 ≤ 5. This is exactly at the threshold.
example_3.py — Small Array
$ Input: nums = [2, 3, 5, 7, 11], threshold = 11
Output: 3
💡 Note: With divisor 3: ceil(2/3) + ceil(3/3) + ceil(5/3) + ceil(7/3) + ceil(11/3) = 1 + 1 + 2 + 3 + 4 = 11 ≤ 11. Perfect match at threshold.

Constraints

  • 1 ≤ nums.length ≤ 5 × 104
  • 1 ≤ nums[i] ≤ 106
  • nums.length ≤ threshold ≤ 106
  • Important: The test cases guarantee that a solution exists

Visualization

Tap to expand
🍕 Pizza Delivery Optimization ProblemPizza Sizes: [1, 2, 5, 9] | Delivery Capacity: 61259Testing 3 Bags per Pizza:3 Bags ConfigurationPizza 1 → ⌈1/3⌉ = 1 bagPizza 2 → ⌈2/3⌉ = 1 bagPizza 5 → ⌈5/3⌉ = 2 bagsPizza 9 → ⌈9/3⌉ = 3 bagsTotal: 7 bags > 6 ❌Testing 5 Bags per Pizza:5 Bags ConfigurationPizza 1 → ⌈1/5⌉ = 1 bagPizza 2 → ⌈2/5⌉ = 1 bagPizza 5 → ⌈5/5⌉ = 1 bagPizza 9 → ⌈9/5⌉ = 2 bagsTotal: 5 bags ≤ 6 ✅Binary Search Process1. Start: left=1, right=92. Test mid=5: sum=5 ≤ 6 ✅3. Update: left=1, right=54. Test mid=3: sum=7 > 6 ❌5. Update: left=4, right=56. Test mid=4: sum=6 ≤ 6 ✅7. Answer: 5 (smallest working)Binary search efficiently finds the minimum viable solution in O(log n) iterations
Understanding the Visualization
1
Set up the delivery problem
Pizzas: [1, 2, 5, 9], Capacity limit: 6 deliveries
2
Try different bag configurations
Test various numbers of bags using binary search
3
Calculate delivery load
For each configuration, count total delivery portions needed
4
Find optimal solution
Minimum bags that keep total deliveries ≤ capacity
Key Takeaway
🎯 Key Insight: The problem has a monotonic property - if divisor X works, then X+1 also works. This makes binary search perfect for finding the minimum solution efficiently.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 24
125.0K Views
Medium-High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen