LC3065. Minimum Operations to Exceed Threshold Value I
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.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 1091 <= k <= 109- The input is generated such that there is at least one index
isuch thatnums[i] >= k.
Approach
func minOperations(nums []int, k int) int {
count := 0
for i := range nums {
if nums[i] < k {
count++
}
}
return count
}Time Complexity O(N)
Space Complexity O(1)
Intuition
The problem requires us to remove elements from the array until all remaining elements are greater than or equal to k. Since we are only allowed to remove the smallest element in each operation, the minimum number of operations corresponds to the count of elements that are strictly less than k.
Algorithm
- Initialize a counter
countto track the number of elements that need to be removed. - Iterate through
nums:- If an element is smaller than
k, incrementcount.
- If an element is smaller than
- Return
countas the result.
Complexity Analysis
- Time Complexity:
O(n)since we iterate through the array once. - Space Complexity:
O(1)because we use only a single variable to track the count.
Wrap up
If you found this guide helpful, consider subscribing to my newsletter on jyotirmoy.dev/blogs , You can also follow me on Twitter jyotirmoydotdev for updates and more content.

