15. Smallest Divisor
✅ GFG solution to the Smallest Divisor problem: find the smallest divisor such that sum of ceiling divisions is ≤ k using binary search. 🚀
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
Given an integer array arr[]
and an integer k
(where k ≥ arr.length
), find the smallest positive integer divisor such that the sum of the ceiling values of each element in arr[]
divided by this divisor is less than or equal to k.
For a divisor d
, the sum is calculated as: ⌈arr[0]/d⌉ + ⌈arr[1]/d⌉ + ... + ⌈arr[n-1]/d⌉ ≤ k
📘 Examples
Example 1
Input: arr[] = [1, 2, 5, 9], k = 6
Output: 5
Explanation: 5 is the smallest divisor having sum of quotients:
⌈1/5⌉ + ⌈2/5⌉ + ⌈5/5⌉ + ⌈9/5⌉ = 1 + 1 + 1 + 2 = 5 ≤ 6
Example 2
Input: arr[] = [1, 1, 1, 1], k = 4
Output: 1
Explanation: 1 is the smallest divisor having sum of quotients:
⌈1/1⌉ + ⌈1/1⌉ + ⌈1/1⌉ + ⌈1/1⌉ = 1 + 1 + 1 + 1 = 4 ≤ 4
🔒 Constraints
$1 \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^6$
$\text{arr.size()} \le k \le 10^6$
✅ My Approach
The optimal approach uses Binary Search on the answer space. Since we need the smallest divisor, we can search in the range [1, max(arr)]
:
Binary Search
Define Search Space:
Lower bound:
l = 1
(smallest positive divisor)Upper bound:
h = max(arr)
(largest possible needed divisor)
Binary Search Logic:
For each mid value
m
, calculate the sum of ceiling divisionsUse the formula:
⌈a/b⌉ = (a + b - 1) / b
for efficient ceiling calculationIf sum ≤ k, try smaller divisors (move right boundary)
If sum > k, need larger divisors (move left boundary)
Ceiling Division Optimization:
Instead of using
ceil(arr[i]/m)
, use(arr[i] + m - 1) / m
This avoids floating-point operations and is more efficient
Termination:
When
l >= h
, we found the smallest valid divisor
📝 Time and Auxiliary Space Complexity
Expected Time Complexity: O(n * log(max_element)), where n is the array size. Binary search runs in O(log(max_element)) iterations, and each iteration requires O(n) time to calculate the sum of ceiling divisions.
Expected Auxiliary Space Complexity: O(1), as we only use constant extra space for variables like left, right, mid, and sum.
🧑💻 Code (C++)
class Solution {
public:
int smallestDivisor(vector<int>& arr, int k) {
int l = 1, h = *max_element(arr.begin(), arr.end()), n = arr.size();
while (l < h) {
int m = l + (h - l) / 2, s = 0;
for (int i = 0; i < n; ++i)
s += (arr[i] + m - 1) / m;
if (s <= k) h = m;
else l = m + 1;
}
return l;
}
};
🧑💻 Code (Java)
class Solution {
int smallestDivisor(int[] arr, int k) {
int l = 1, h = Arrays.stream(arr).max().getAsInt();
while (l < h) {
int m = l + (h - l) / 2, s = 0;
for (int x : arr)
s += (x + m - 1) / m;
if (s <= k) h = m;
else l = m + 1;
}
return l;
}
}
🐍 Code (Python)
class Solution:
def smallestDivisor(self, arr, k):
l, h = 1, max(arr)
while l < h:
m, s = l + (h - l) // 2, 0
for x in arr:
s += (x + m - 1) // m
if s <= k:
h = m
else:
l = m + 1
return l
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let's make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated