Skip to main content

Minimum K to Reduce Array Within Limit - Solution & Explanation

MediumArrayBinary Search8 min readAsked at: Google
Practice this problem

Problem Statement

You are given a positive integer array nums.

For a positive integer k, define nonPositive(nums, k) as the minimum number of operations needed to make every element of nums non-positive. In one operation, you can choose an index i and reduce nums[i] by k.

Return an integer denoting the minimum value of k such that nonPositive(nums, k) <= k2.

 

Example 1:

Input: nums = [3,7,5]

Output: 3

Explanation:

When k = 3, nonPositive(nums, k) = 6 <= k2.

  • Reduce nums[0] = 3 one time. nums[0] becomes 3 - 3 = 0.
  • Reduce nums[1] = 7 three times. nums[1] becomes 7 - 3 - 3 - 3 = -2.
  • Reduce nums[2] = 5 two times. nums[2] becomes 5 - 3 - 3 = -1.

Example 2:

Input: nums = [1]

Output: 1

Explanation:

When k = 1, nonPositive(nums, k) = 1 <= k2.

  • Reduce nums[0] = 1 one time. nums[0] becomes 1 - 1 = 0.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

Approach Overview

Problem Overview: You are given an integer array and a limit on the number of operations allowed to reduce it. Each operation reduces an element by up to k. The goal is to find the minimum value of k such that the entire array can be reduced within the allowed limit of operations.

Approach 1: Linear Search on k (O(n * M) time, O(1) space)

Start from k = 1 and keep increasing it until the required number of operations becomes less than or equal to the allowed limit. For each candidate k, iterate through the array and compute how many operations each element requires using a ceiling-style division like ceil(nums[i] / k). Sum the operations and check if it stays within the limit. This method is easy to reason about but inefficient because k may need to be tested up to the maximum value in the array.

Approach 2: Binary Search on k (O(n log M) time, O(1) space)

The key observation is monotonic behavior: if a certain k allows the array to be reduced within the limit, any larger k will also work because each operation removes more value. This makes the answer searchable using Binary Search. Set the search range from 1 to max(nums). For each midpoint k, iterate through the array and calculate the total operations required using (num + k - 1) / k. If the total operations exceed the limit, increase k. Otherwise, try a smaller k to find the minimum valid value.

The feasibility check runs in O(n), and binary search performs log M iterations where M is the maximum element in the array. This produces an overall complexity of O(n log M) with constant extra space.

Recommended for interviews: The Binary Search approach. Interviewers expect you to recognize the monotonic relationship between k and the number of operations required. Demonstrating the brute-force reasoning first shows understanding of the constraint, while converting it into a binary search over the answer shows strong problem-solving skill.

Solution

We notice that as k increases, it becomes easier to satisfy the condition. This exhibits monotonicity, so we can use binary search to find the minimum k.

We define the left boundary of the binary search as l = 1 and the right boundary as r = 10^5. In each binary search iteration, we calculate the middle value mid = \lfloor (l + r) / 2 \rfloor and determine whether the condition nonPositive(nums, k) leq k^2 is satisfied when k = mid. If the condition is satisfied, we update the right boundary to r = mid; otherwise, we update the left boundary to l = mid + 1. When the binary search ends, the left boundary l is the minimum k we are looking for.

The time complexity is O(n log M), where n and M are the length of the array nums and the maximum range respectively. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Linear Search on kO(n * M)O(1)Useful for reasoning about the problem or when the maximum value in the array is very small
Binary Search on AnswerO(n log M)O(1)General case; optimal when k lies within a large numeric range

Video Solution

Minimum K to Reduce Array Within Limit 🔥 LeetCode 3824 | Biweekly Contest 175 | Binary Search + Math • Study Placement • 264 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum K to Reduce Array Within Limit easy or hard?
The problem is typically considered Medium difficulty. The main challenge is recognizing that the answer space for k is monotonic and can be searched with binary search, reducing the complexity from a potentially large brute-force search to O(n log M).
Minimum K to Reduce Array Within Limit Python/Java solution
Implement binary search between 1 and the maximum element of the array. For each midpoint k, iterate through the array and accumulate required operations using ceiling division. If the operations exceed the limit, move the left boundary; otherwise store the result and shrink the right boundary.
How to solve Minimum K to Reduce Array Within Limit in O(n log M)?
Use binary search over the value of k. For each candidate k, iterate through the array and compute how many operations each element needs using ceiling division such as (num + k - 1) / k. If the total operations exceed the allowed limit, increase k; otherwise continue searching for a smaller valid k.
What is the best approach for Minimum K to Reduce Array Within Limit?
Binary search on the answer is the most efficient approach. The number of operations required decreases as k increases, which creates a monotonic search space. By checking feasibility for each candidate k and adjusting the search range, you find the minimum valid k in O(n log M) time where M is the maximum element in the array.
Is Minimum K to Reduce Array Within Limit asked at Google/Amazon/Meta?
Problems that use binary search on the answer are common in interviews at companies like Google, Amazon, and Meta. Variants appear frequently in coding interviews because they test recognition of monotonic conditions and efficient feasibility checks.
What data structure is used in Minimum K to Reduce Array Within Limit?
The solution mainly uses a simple array traversal combined with binary search on a numeric range. No complex data structures are required; the algorithm repeatedly scans the array to compute how many operations are needed for a given k.
What is the time complexity of Minimum K to Reduce Array Within Limit?
The optimal solution runs in O(n log M) time. Each binary search step checks the array once to compute the number of required operations, which costs O(n). The binary search itself runs for log M iterations where M is the maximum value in the array.

Ready to solve this problem?

Practice Minimum K to Reduce Array Within Limit with our built-in code editor and test cases.

Practice on FleetCode