BackmediumBinary Searchuncategorizedmedium

Minimum Threshold for Resource Allocation Solution

Problem Statement

You are given an array of positive integers rates of length n, and an integer k. You need to find the minimum positive integer threshold M such that the sum of ceil(rates[i] / M) for all 0 <= i < n is less than or equal to k.

Here, ceil(x) represents the ceiling function, which returns the smallest integer greater than or equal to x.

If no such positive integer M exists (which occurs when k is less than n), return -1.

Example 1
Input
[3, 6, 7, 11, 6], 8
Output
-1

Explanation: Step-by-step: Given the input array [3, 6, 7, 11, 6] and k = 8, we need to find the minimum positive integer threshold M. We start with M = 1 and calculate the sum ceil(rates[i] / M) for all 0 <= i < n. For M = 1, the sum is ceil(3/1) + ceil(6/1) + ceil(7/1) + ceil(11/1) + ceil(6/1) = 3 + 6 + 7 + 11 + 6 = 33, which is greater than 8. Since no such positive integer M exists, the output should be -1.

Example 2
Input
[1, 2, 3, 4, 5], 5
Output
5

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5] and k = 5, we need to find the minimum positive integer threshold M. We start with M = 1 and calculate the sum ceil(rates[i] / M) for all 0 <= i < n. For M = 1, the sum is ceil(1/1) + ceil(2/1) + ceil(3/1) + ceil(4/1) + ceil(5/1) = 1 + 2 + 3 + 4 + 5 = 15, which is greater than 5. We then try M = 2, and the sum is ceil(1/2) + ceil(2/2) + ceil(3/2) + ceil(4/2) + ceil(5/2) = 0 + 1 + 1 + 2 + 2 = 6, which is still greater than 5. We continue this process until we find the minimum positive integer threshold M that satisfies the condition. In this case, the output should be 5.

Constraints

  • 1 <= rates.length <= 10^5
  • 1 <= rates[i] <= 10^9
  • 1 <= k <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Threshold for Resource Allocation — Problem Statement & Solution Guide

Binary SearchMediumMixed
TimeO(n * log(maxRate))
|
SpaceO(1)

Problem Description

You are given an array of positive integers rates of length n, and an integer k. You need to find the minimum positive integer threshold M such that the sum of ceil(rates[i] / M) for all 0 <= i < n is less than or equal to k.

Here, ceil(x) represents the ceiling function, which returns the smallest integer greater than or equal to x.

If no such positive integer M exists (which occurs when k is less than n), return -1.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Minimum Threshold for Resource Allocation"

medium

WHY DOES IT MATTER?

Binary search on answer is a powerful pattern for any optimization problem where the feasibility of a candidate can be checked in polynomial time and the feasibility predicate is monotonic. Recognizing this pattern lets you turn an exponential‑or‑linear‑search space into a logarithmic one.

OPTIMIZATION CHALLENGE

The key insight is that ⌈a/b⌉ can be computed without floating‑point division as (a + b - 1) / b, and that the sum can be aborted early when it exceeds k, cutting unnecessary work in each binary‑search iteration.

REAL-WORLD CONNECTION

Think of allocating bandwidth to users: each user needs a certain amount of data (rates[i]). The threshold M is the maximum chunk size you allow per transmission. You want the smallest chunk size that still keeps the total number of transmissions under a service‑level agreement (k). This mirrors rate‑limiting and packet‑sizing in distributed systems.

During an interview, write the feasibility function first, test it on edge cases (k < n, k >= sum of rates), and then wrap a clean binary‑search loop. Keep the loop invariant clear: low is infeasible, high is feasible.

COMPLEXITY AT A GLANCE

⏱ Time:O(n * log(maxRate))
💾 Space:O(1)

Core Theory — Why This Approach?

The problem asks for the smallest integer M that caps the total number of "chunks" needed when each rate[i] is split into pieces of size at most M. The function f(M)=∑⌈rates[i]/M⌉ is monotonic decreasing with respect to M: as M grows, each ceiling term can only stay the same or drop, never increase. This monotonicity enables a binary search over the answer space. A naïve solution would try every possible M from 1 to max(rates) and compute the sum each time, leading to O(n·max(rates)) time, which is infeasible when rates contain values up to 10⁹. By exploiting the monotone property we can locate the boundary where f(M)≤k in O(log max(rates)) iterations, each costing O(n) to evaluate, yielding an overall O(n log max(rates)) algorithm. The optimal paradigm is therefore "binary search on answer", a classic technique for minimization problems with a monotone predicate.

Interview Questions on This Problem

Q1How would you modify the solution if the rates array could contain zero values?

Zero values contribute 0 to the sum regardless of M, because ⌈0/M⌉ = 0. The binary‑search predicate remains unchanged; we just need to ensure we don’t divide by zero (M is always ≥1) and that the lower bound of the answer space stays at 1.

Q2What is the time complexity if you pre‑sort the rates array before binary searching?

Sorting adds O(n log n) overhead, but the per‑iteration sum can be computed faster by early‑stopping once the cumulative sum exceeds k. The overall complexity becomes O(n log n + n log maxRate), which is asymptotically the same as the unsorted version for large inputs.

Q3Explain how you would adapt the algorithm for a streaming input where rates are read one by one and you cannot store the entire array.

Maintain the current sum while reading each rate and compute ⌈rate/M⌉ on the fly for a given candidate M. Since binary search needs multiple passes, you would have to store the stream or replay it; alternatively, you can perform a parametric search using a double‑ended queue of partial sums, but the simplest adaptation is to read the stream into memory because the binary‑search predicate requires random access to all elements.

Examples

Example 1

Input

[3, 6, 7, 11, 6], 8

Output

-1

Explanation: Step-by-step: Given the input array [3, 6, 7, 11, 6] and k = 8, we need to find the minimum positive integer threshold M. We start with M = 1 and calculate the sum ceil(rates[i] / M) for all 0 <= i < n. For M = 1, the sum is ceil(3/1) + ceil(6/1) + ceil(7/1) + ceil(11/1) + ceil(6/1) = 3 + 6 + 7 + 11 + 6 = 33, which is greater than 8. Since no such positive integer M exists, the output should be -1.

Example 2

Input

[1, 2, 3, 4, 5], 5

Output

5

Explanation: Step-by-step: Given the input array [1, 2, 3, 4, 5] and k = 5, we need to find the minimum positive integer threshold M. We start with M = 1 and calculate the sum ceil(rates[i] / M) for all 0 <= i < n. For M = 1, the sum is ceil(1/1) + ceil(2/1) + ceil(3/1) + ceil(4/1) + ceil(5/1) = 1 + 2 + 3 + 4 + 5 = 15, which is greater than 5. We then try M = 2, and the sum is ceil(1/2) + ceil(2/2) + ceil(3/2) + ceil(4/2) + ceil(5/2) = 0 + 1 + 1 + 2 + 2 = 6, which is still greater than 5. We continue this process until we find the minimum positive integer threshold M that satisfies the condition. In this case, the output should be 5.

Constraints

  • 1 <= rates.length <= 10^5
  • 1 <= rates[i] <= 10^9
  • 1 <= k <= 10^9

Optimal Approach & Strategy

Perform a binary search on M in the range [1, max(rates)], using a helper that computes the sum of (rates[i] + M - 1) / M and aborts early if the sum exceeds k.

Brute Force Approach

Iterate M from 1 up to max(rates), compute the total ceil sum for each M, and stop when the sum ≤ k.

Verified Code Solutions

JavaScript Solution
Time: O(n * log(maxRate))
function solution(rates, k) {
   let M = 1;
   while (true) {
       let sum = 0;
       for (let rate of rates) {
           sum += Math.ceil(rate / M);
       }
       if (sum <= k) {
           return M;
       }
       M++;
   }
   return -1;
}

Asked in Top Tech Interviews

uncategorizedmediumnone

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.