484. Find the Smallest Divisor Given a Threshold

MediumBinary SearchBinary Search on AnswerArray

Given an array and a threshold, choose a positive integer divisor and divide every element by it, rounding each result up to the nearest integer, then sum these. Return the smallest divisor such that this sum is less than or equal to the threshold. Input: '[arr], threshold'.

Input: '[arr], threshold'.

Output: Integer — the smallest valid divisor.

Examples

Example 1
Input: [1,2,5,9], 6
Output: 5
Explanation: Divisor 5 gives sum 1+1+1+2=5<=6.
Example 2
Input: [44,22,33,11,1], 5
Output: 44
Explanation: Large divisor needed.
Example 3
Input: [2,3,5,7,11], 11
Output: 3
Explanation: Smallest divisor with sum<=11.

Constraints

Asked by

IBMAmazonOracleGoogleMicrosoftMeta
Solve this problem in the editor →