Minimum Operations to Exceed Threshold Value I - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,11,10,1,3], k = 10
Output: 3
💡 Note: Elements less than 10 are [2,1,3]. We need to remove all 3 of them, so answer is 3.
Example 2 — All Elements Valid
$ Input: nums = [1,1,2,4,9], k = 1
Output: 0
💡 Note: All elements are already ≥ 1, so no operations needed.
Example 3 — Remove All Elements
$ Input: nums = [2,4,6,8], k = 10
Output: 4
💡 Note: All elements are less than 10, so we need to remove all 4 elements.

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ 109

Visualization

Tap to expand
Minimum Operations to Exceed Threshold Value I INPUT nums = [2, 11, 10, 1, 3] 2 idx 0 11 idx 1 10 idx 2 1 idx 3 3 idx 4 Threshold k = 10 All elements must be >= 10 Compare each element: 2 < 10 (remove) 11 >= 10 (keep) 10 >= 10 (keep) 1 < 10 (remove) 3 < 10 (remove) ALGORITHM STEPS 1 Initialize counter count = 0 2 Iterate through array For each element in nums 3 Check condition If element < k, increment count 4 Return count Number of elements to remove Counting Process: nums[0]=2 < 10? Yes, count=1 nums[1]=11 < 10? No, count=1 nums[2]=10 < 10? No, count=1 nums[3]=1 < 10? Yes, count=2 nums[4]=3 < 10? Yes, count=3 FINAL RESULT Elements to remove (marked red): 2 REMOVE 11 KEEP 10 KEEP 1 REMOVE 3 REMOVE OUTPUT 3 3 operations needed Remove: 2, 1, 3 Remaining: [11, 10] All elements >= 10 -- OK Key Insight: The optimized approach counts elements below threshold k in O(n) time with O(1) space. No need to sort or track order - simply count how many elements are less than k. TutorialsPoint - Minimum Operations to Exceed Threshold Value I | Count Elements Below Threshold Approach
Asked in
Amazon 15 Google 12
12.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen