Find the Smallest Divisor Given a Threshold - Problem
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor, divide all the array elements by it, and sum the division's result.
Find the smallest divisor such that the result mentioned above is less than or equal to threshold.
Each result of the division is rounded to the nearest integer greater than or equal to that element (ceiling function). For example: 7/3 = 3 and 10/2 = 5.
The test cases are generated so that there will be an answer.
Input & Output
Example 1 — 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 give larger sums, so 5 is the answer.
Example 2 — Larger Threshold
$
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
Example 3 — 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
Constraints
- 1 ≤ nums.length ≤ 5 × 104
- 1 ≤ nums[i] ≤ 106
- nums.length ≤ threshold ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [1,2,5,9] with threshold 6
2
Process
Test divisors and calculate ceiling division sums
3
Output
Smallest divisor where sum ≤ threshold
Key Takeaway
🎯 Key Insight: As divisor increases, the sum decreases monotonically, making binary search the perfect optimization
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code