← DiffPush

Find smallest integer

Baseline Binary Search · In Search Space O(n log M), where M is the largest element · O(1)

Choosing the Coarsest Sampler That Still Resolves the Signal

A signal pipeline compresses each sample by integer division before summing the channels, and the regulator caps the combined output. A coarser divisor lowers the output but loses detail, so the team wants the smallest divisor that keeps the sum within the cap. Pick that divisor.

Input: An array nums of n positive integers and an integer threshold.

Output: The smallest positive divisor d such that the sum of ceil(nums[i] / d) over all i is at most threshold.

Constraints

Examples

Example 1

Input: {"nums":[1,2,5,9],"threshold":6}
Output: 5
Divisor 4 leaves a sum of 7, while divisor 5 rounds every channel down far enough for a total of 5.

Example 2

Input: {"nums":[44,22,33,11,1],"threshold":5}
Output: 44
Every sample must collapse to 1, which happens only at a divisor of 44, the largest value.

Solve this in your browser →

Also on LeetCode ↗