Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Smallest Divisor Given A Threshold

Medium Acceptance 67.00% Points 30.00

You are given an array of integers nums[] and an integer threshold. You must choose a positive integer divisor and divide each element in the array by it. The result of each division is rounded up to the nearest integer. Return the smallest divisor such that the sum of all the division results is less than or equal to the threshold.

Examples
Example 1

Example 1

Input: nums = [1,2,5,9], threshold = 6

Output: 5

Explanation: If divisor = 5 ceil(1/5) = 1 ceil(2/5) = 1 ceil(5/5) = 1 ceil(9/5) = 2 Sum = 1 + 1 + 1 + 2 = 5 = 6 This is the smallest divisor that satisfies the condition.

Example 2

Example 2

Input: nums = [44,22,33,11,1], threshold = 5

Output: 44

Explanation: If divisor = 44 ceil(44/44) = 1 ceil(22/44) = 1 ceil(33/44) = 1 ceil(11/44) = 1 ceil(1/44) = 1 Sum = 5 = threshold This is the smallest divisor that satisfies the condition.

Example 3

Example 3

Input: nums = [2,3,5,7,11], threshold = 11

Output: 3

Explanation: If divisor = 3 ceil(2/3) = 1 ceil(3/3) = 1 ceil(5/3) = 2 ceil(7/3) = 3 ceil(11/3) = 4 Sum = 11 = threshold This is the smallest divisor that satisfies the condition.

Hints
Hint 1
N/A
Constraints
  • 1 = nums.length = 5 × 104
  • 1 = nums[i] = 106
  • nums.length = threshold = 106
Companies
Apple Samsung Visa Walmart
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions