Advertisement

Find the Smallest Divisor Given a Threshold - LeetCode 1283 Solution

Find the Smallest Divisor Given a Threshold - Complete Solution Guide

Find the Smallest Divisor Given a Threshold is LeetCode problem 1283, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of integers nums and an integer threshold , we will choose a positive integer divisor , divide all the array by it, and sum the division's result. Find the smallest divisor such that the result mentioned above is less than or equal to threshold . Each result of the division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5 ). The test cases are generated so that there will be an answer. Example 1: Input: nums = [1,2,5,9], t

Detailed Explanation

The problem requires finding the smallest positive integer divisor that, when used to divide each number in a given array `nums`, results in a sum of the rounded-up quotients that is less than or equal to a given `threshold`. In simpler terms, we need to find the smallest divisor such that the sum of `ceil(num / divisor)` for all `num` in `nums` is at most `threshold`. The division result is always rounded up to the nearest integer.

Solution Approach

The solution uses binary search to find the smallest divisor. It starts with a search range from 1 (minimum divisor) to the maximum value in the input array `nums` (maximum divisor). In each iteration of the binary search, the midpoint `mid` is chosen as a potential divisor. The sum of the rounded-up quotients (using the formula `(num + mid - 1) // mid`) is calculated for all numbers in `nums`. If this sum is less than or equal to the `threshold`, it means we can potentially find a smaller divisor, so the upper bound of the search range is updated to `mid`. Otherwise, the sum exceeds the threshold, indicating that the divisor is too small, so the lower bound of the search range is updated to `mid + 1`. This process continues until the lower and upper bounds converge, at which point the lower bound represents the smallest divisor that satisfies the given condition.

Step-by-Step Algorithm

  1. Step 1: Initialize `low` to 1 (minimum possible divisor) and `high` to the maximum element in `nums` (maximum possible divisor).
  2. Step 2: While `low` is less than `high`, calculate the middle value `mid` as `(low + high) // 2`.
  3. Step 3: Calculate `current_sum` by iterating through `nums` and summing the rounded-up quotients `(num + mid - 1) // mid`.
  4. Step 4: If `current_sum` is less than or equal to `threshold`, update `high` to `mid` to search for a potentially smaller divisor in the lower half.
  5. Step 5: Otherwise, if `current_sum` is greater than `threshold`, update `low` to `mid + 1` to search for a larger divisor in the upper half.
  6. Step 6: After the loop finishes (when `low` is equal to `high`), `low` (or `high`) will contain the smallest divisor that satisfies the condition. Return `low`.

Key Insights

  • Insight 1: The problem can be solved using Binary Search because the result of dividing each element by the divisor and summing them up is monotonically decreasing as the divisor increases. This monotonicity property allows us to efficiently search for the smallest divisor.
  • Insight 2: The minimum possible divisor is 1, and the maximum possible divisor is the largest number in the `nums` array. These boundaries define the search space for the binary search.
  • Insight 3: The ceiling operation `ceil(num / divisor)` can be efficiently calculated using `(num + divisor - 1) // divisor` using integer division, avoiding floating-point calculations which could introduce inaccuracies.

Complexity Analysis

Time Complexity: O(nlog(m))

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search.

Companies

Asked at: Agoda, DE Shaw, Expedia, Millennium, PayPal, ZScaler.