🧠
Binary Search with Helper Feasibility Function
💡 This approach separates the feasibility check into a helper function for cleaner code and better readability, which is a good practice in interviews.
Intuition
Use a helper function to check if a given divisor satisfies the threshold condition, then binary search over divisors using this helper.
Algorithm
- Define a helper function that returns true if sum of ceil(nums[i]/divisor) ≤ threshold.
- Initialize low = 1 and high = max(nums).
- While low < high:
- mid = (low + high) // 2
- Use helper to check feasibility with mid.
- If feasible, set high = mid; else low = mid + 1.
- Return low.
💡 Modularizing feasibility makes the binary search logic clearer and easier to debug.
import math
def is_feasible(nums, divisor, threshold):
total = 0
for num in nums:
total += math.ceil(num / divisor)
if total > threshold:
return False
return True
def smallest_divisor(nums, threshold):
low, high = 1, max(nums)
while low < high:
mid = (low + high) // 2
if is_feasible(nums, mid, threshold):
high = mid
else:
low = mid + 1
return low
# Driver code
if __name__ == "__main__":
nums = [1, 2, 5, 9]
threshold = 6
print(smallest_divisor(nums, threshold)) # Output: 5
Line Notes
def is_feasible(nums, divisor, threshold):Defines a helper to check if divisor meets threshold
if total > threshold:Early exit to improve efficiency when sum exceeds threshold
if is_feasible(nums, mid, threshold):Uses helper to decide binary search direction
low, high = 1, max(nums)Initialize binary search bounds
import java.util.*;
public class SmallestDivisor {
private static boolean isFeasible(int[] nums, int divisor, int threshold) {
int total = 0;
for (int num : nums) {
total += (num + divisor - 1) / divisor;
if (total > threshold) {
return false;
}
}
return true;
}
public static int smallestDivisor(int[] nums, int threshold) {
int low = 1, high = 0;
for (int num : nums) {
high = Math.max(high, num);
}
while (low < high) {
int mid = low + (high - low) / 2;
if (isFeasible(nums, mid, threshold)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
public static void main(String[] args) {
int[] nums = {1, 2, 5, 9};
int threshold = 6;
System.out.println(smallestDivisor(nums, threshold)); // Output: 5
}
}
Line Notes
private static boolean isFeasible(int[] nums, int divisor, int threshold)Helper method to check feasibility
if (total > threshold)Early return to avoid unnecessary computation
if (isFeasible(nums, mid, threshold))Binary search decision based on helper result
int low = 1, high = 0;Initialize binary search bounds
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isFeasible(const vector<int>& nums, int divisor, int threshold) {
int total = 0;
for (int num : nums) {
total += (num + divisor - 1) / divisor;
if (total > threshold) {
return false;
}
}
return true;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int low = 1, high = *max_element(nums.begin(), nums.end());
while (low < high) {
int mid = low + (high - low) / 2;
if (isFeasible(nums, mid, threshold)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
int main() {
vector<int> nums = {1, 2, 5, 9};
int threshold = 6;
cout << smallestDivisor(nums, threshold) << endl; // Output: 5
return 0;
}
Line Notes
bool isFeasible(const vector<int>& nums, int divisor, int threshold)Helper function to check feasibility
if (total > threshold)Early exit to improve performance
if (isFeasible(nums, mid, threshold))Binary search uses helper to decide direction
int low = 1, high = *max_element(nums.begin(), nums.end());Initialize binary search range
function isFeasible(nums, divisor, threshold) {
let total = 0;
for (const num of nums) {
total += Math.floor((num + divisor - 1) / divisor);
if (total > threshold) {
return false;
}
}
return true;
}
function smallestDivisor(nums, threshold) {
let low = 1, high = Math.max(...nums);
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (isFeasible(nums, mid, threshold)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
// Test
console.log(smallestDivisor([1, 2, 5, 9], 6)); // Output: 5
Line Notes
function isFeasible(nums, divisor, threshold)Helper function to check if divisor is valid
if (total > threshold)Early return to avoid unnecessary work
if (isFeasible(nums, mid, threshold))Binary search decision based on helper
let low = 1, high = Math.max(...nums);Initialize binary search bounds
TimeO(n log(max(nums)))
SpaceO(1)
Same as previous approach but with clearer code structure and early exits in feasibility check.
💡 This approach is easier to maintain and debug, which is valuable in interviews.
Interview Verdict: Accepted
This is the recommended approach for interviews due to clarity and efficiency.