Find the Smallest Divisor
A medium Binary Search problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Search
- Sheets
- 1
- Core for
- 7 roles
- Platform
- LeetCode
The problem
Given an array of positive integers and a threshold, find the smallest divisor such that the sum of dividing each element by the divisor (rounding up) is less than or equal to the threshold.
Example 1
- Input
- nums = [1,2,5,9], threshold = 6
- Output
- 5
- Why
- With divisor 5: ceil(1/5)+ceil(2/5)+ceil(5/5)+ceil(9/5) = 1+1+1+2 = 5 <= 6. With divisor 4: 1+1+2+3 = 7 > 6.
Example 2
- Input
- nums = [44,22,33,11,1], threshold = 5
- Output
- 44
- Why
- 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.
Constraints
- 1 <= nums.length <= 5 * 10^4
- 1 <= nums[i] <= 10^6
- nums.length <= threshold <= 10^6
How to think about it
Updated 2026-09-09Dividing by a larger divisor reduces or preserves each individual division result, so the grand sum of ceil divisions is monotonically non-increasing as the divisor grows. The first divisor that pushes the total sum down to or below the threshold is the boundary of a monotonic binary search.
Approaches, worst first
Sequential divisor scan
time O(max(nums) * n) · space O(1)
Test divisor starting from 1 upward and calculate the sum until the condition is met. Correct because the sum decreases monotonically, but takes up to 10^6 tests when numbers are large.
Binary search on divisor valueWrite this one
time O(n log(max(nums))) · space O(1)
Search candidate divisors in the range from 1 to max(nums). For mid, compute the sum of (val + mid - 1) / mid. If sum <= threshold, try smaller divisors (high = mid); otherwise advance low = mid + 1.
Where people lose marks · 3
- Starting the divisor search range at 0 leads to division-by-zero runtime exceptions; the minimum possible divisor is 1.
- Using standard floating-point division with Math.ceil introduces precision loss on large numbers; integer ceiling formula (x + d - 1) / d avoids float errors.
- Setting the maximum divisor boundary to threshold instead of max(nums); when all elements equal 10^6 and threshold equals n, the required divisor must reach max(nums).
The theory behind it
Binary Search — the ground this problem stands on. All Binary Search problems
What Binary Search is
Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.
When to reach for it
Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.
How the pattern works
Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.
What each operation costs
| Operation | Time |
|---|---|
| find target in sorted array | O(log n) |
| find boundary in monotonic range | O(log n) |
What usually goes wrong with Binary Search
- Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
- Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
- Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.
Which roles need this problem
Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Search problems
Problem set and role mapping as of .