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
Find Smallest Divisor: [1,2,5,9] with threshold = 61259Test divisor = 3:ceil(1/3) = 1ceil(2/3) = 1ceil(5/3) = 2ceil(9/3) = 3Sum = 1 + 1 + 2 + 3 = 7 > 6 ✗Test divisor = 5:ceil(1/5) = 1ceil(2/5) = 1ceil(5/5) = 1ceil(9/5) = 2Sum = 1 + 1 + 1 + 2 = 5 ≤ 6 ✓Answer: 5
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
Asked in
Amazon 15 Facebook 12 Google 8
87.4K Views
Medium Frequency
~25 min Avg. Time
2.2K 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