Watch 10 video solutions for Minimum Operations to Exceed Threshold Value I, a easy level problem involving Array. This walkthrough by CodeJulian has 597 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed integer array nums, and an integer k.
In one operation, you can remove one occurrence of the smallest element of nums.
Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
Example 1:
Input: nums = [2,11,10,1,3], k = 10 Output: 3 Explanation: After one operation, nums becomes equal to [2, 11, 10, 3]. After two operations, nums becomes equal to [11, 10, 3]. After three operations, nums becomes equal to [11, 10]. At this stage, all the elements of nums are greater than or equal to 10 so we can stop. It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.
Example 2:
Input: nums = [1,1,2,4,9], k = 1 Output: 0 Explanation: All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.
Example 3:
Input: nums = [1,1,2,4,9], k = 9 Output: 4 Explanation: only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 1091 <= k <= 109i such that nums[i] >= k.Problem Overview: You are given an integer array nums and a threshold value k. One operation removes the smallest element from the array. The goal is to find the minimum number of operations required so that every remaining element in the array is at least k.
Approach 1: Sort and Count (O(n log n) time, O(1) extra space)
This approach sorts the array first, which places the smallest elements at the beginning. After sorting, iterate from the start of the array and count how many values are strictly less than k. Each such element must be removed because it can never satisfy the threshold condition. The moment you encounter a value >= k, you stop counting since all remaining elements will also meet the requirement. Sorting costs O(n log n), and the scan is O(n). This approach is straightforward and works well when modifying the array order is acceptable.
Approach 2: Use a Min-Heap (O(n log n) time, O(n) space)
A min-heap keeps the smallest element accessible at all times. Insert all elements of nums into a heap, then repeatedly check the top element using peek(). If the smallest value is less than k, remove it using poll() and increment the operation count. Continue until the smallest value in the heap becomes >= k or the heap becomes empty. Heap construction takes O(n), while each removal costs O(log n). In the worst case you remove many elements, giving O(n log n) time and O(n) space.
Both approaches rely on identifying values below the threshold. The difference lies in how the smallest element is accessed. Sorting gives a static order and a single linear scan. A heap maintains dynamic ordering and repeatedly exposes the minimum element.
Conceptually, the task is a simple filtering problem over an array. The heap solution introduces a heap data structure, while the first solution relies on sorting to organize elements before counting.
Recommended for interviews: The core insight is that every element smaller than k must be removed. Many candidates immediately sort and count, which clearly demonstrates the idea and runs in O(n log n). Stronger candidates often notice that you can simply iterate once and count elements less than k, achieving O(n) time and O(1) space. Interviewers mainly look for recognition that operations correspond directly to elements below the threshold.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sort and Count | O(n log n) | O(1) | Simple implementation when modifying array order is fine |
| Min-Heap | O(n log n) | O(n) | Useful when repeatedly accessing the smallest element dynamically |
| Linear Scan (Optimal Insight) | O(n) | O(1) | Best when you only need to count values less than k |