Find the Smallest Divisor Given a Threshold - Problem
Find the Smallest Divisor Given a Threshold
You're given an array of integers
Here's the catch: each division result must be rounded up to the nearest integer (ceiling function). For example:
•
•
•
Goal: Find the minimum divisor that keeps the sum within the threshold limit. The problem guarantees a solution exists.
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 1Goal: 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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code