Find the Smallest Divisor Given a Threshold

MediumBinary Searchbinary search on answer

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums and an integer threshold, choose a positive integer divisor d. Divide every element by d, rounding each result **up** to the nearest integer, and add the results together. Return the smallest d for which that sum is less than or equal to threshold. The input guarantees threshold >= nums.length, so an answer always exists (dividing by max(nums) makes every term 1).

Examples

Input: nums = [1,2,5,9], threshold = 6
Output: 5
With d = 5 the sum is 1 + 1 + 1 + 2 = 5; with d = 4 it is 1 + 1 + 2 + 3 = 7, which is too large.
Input: nums = [44,22,33,11,1], threshold = 5
Output: 44
Only a divisor of at least 44 turns every element into 1.

Constraints

  • `1 <= nums.length <= 5 * 10^4`
  • `1 <= nums[i] <= 10^6`
  • `nums.length <= threshold <= 10^6`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search on answer problems