Minimum K to Reduce Array Within Limit - Solution & Explanation
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] = 3one time.nums[0]becomes3 - 3 = 0. - Reduce
nums[1] = 7three times.nums[1]becomes7 - 3 - 3 - 3 = -2. - Reduce
nums[2] = 5two times.nums[2]becomes5 - 3 - 3 = -1.
Example 2:
Input: nums = [1]
Output: 1
Explanation:
When k = 1, nonPositive(nums, k) = 1 <= k2.
- Reduce
nums[0] = 1one time.nums[0]becomes1 - 1 = 0.
Constraints:
1 <= nums.length <= 1051 <= 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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Linear Search on k | O(n * M) | O(1) | Useful for reasoning about the problem or when the maximum value in the array is very small |
| Binary Search on Answer | O(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?
Minimum K to Reduce Array Within Limit Python/Java solution
How to solve Minimum K to Reduce Array Within Limit in O(n log M)?
What is the best approach for Minimum K to Reduce Array Within Limit?
Is Minimum K to Reduce Array Within Limit asked at Google/Amazon/Meta?
What data structure is used in Minimum K to Reduce Array Within Limit?
What is the time complexity of Minimum K to Reduce Array Within Limit?
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