Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Find Smallest Divisor Given A Threshold

Medium Acceptance 67% Points 30

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.

Constraints
  • 1 = nums.length = 5 × 104
  • 1 = nums[i] = 106
  • nums.length = threshold = 106
Topics
Binary Search
Companies
Apple Samsung Visa Walmart
Added 19 Jul 2026 67% acceptance
Ln 1, Col 1Sign in to save your code
1 2 5 9
6
5