Talentd
FleetCode
Make and follow sheets.Sign in

Minimum Operations to Exceed Threshold Value I solution and explanation

EasyArray5 min read86.7% acceptanceUpdated 9 Apr 2026
3065 Minimum Operations to Exceed Threshold Value I || Sorting ✅ || C++ || Java || PythonAyush Rao · 3:10 · 322 viewsWatch on YouTube
9 more videos on this problem
  1. 3065 Minimum Operations to Exceed Threshold Value I || Sorting ✅ || C++ || Java || PythonAyush Rao · 3:10 · 322 views
  2. LeetCode | 3065. Minimum Operations to Exceed Threshold Value I | Biweekly Contest 125Coding with Bobby · 1:20 · 192 views
  3. 3065. Minimum Operations to Exceed Threshold Value I (Leetcode Easy)Programming Live with Larry · 3:20 · 190 views
  4. my attempt at solving leetcode 3065: minimum operations to exceed threshold value i [SOLVED]Cameron Late · 4:06 · 123 views
  5. Leetcode easy problem 3065. Minimum Operations to Exceed Threshold Value I | devops python practiceADevOpsEngineer · 1:40 · 114 views
  6. 3065. Minimum Operations to Exceed Threshold Value I || Leetcode Biweekly Contest 125Coding Club RVCE · 4:37 · 99 views
  7. Day 110: LeetCode Problem 3065. Minimum Operations to Exceed Threshold Value I - SwiftiOSxBank · 8:37 · 36 views
  8. Minimum Operations to Exceed Threshold Value I - LeetCode with Python, Rust, and JavaScript0xkernel · 1:41 · 19 views
  9. 3065. Minimum Operations to Exceed Threshold Value Ikahan ghori · 1:10 · 12 views
  10. 3065 Minimum Operations to Exceed Threshold Value I (Python)bromleyp · 2:17 · 9 views

The 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.

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 <= 50
  • 1 <= nums[i] <= 109
  • 1 <= k <= 109
  • The input is generated such that there is at least one index i such that nums[i] >= k.

Approach overview

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 1: Sort and Count Approach

Sort the array first. Start from the smallest element and keep a count of numbers less than k. Remove these elements until all remaining numbers are >= k, counting the number of operations.

def min_operations(nums, k):
    nums.sort()
    count = 0
    for num in nums:
        if num >= k:
            break
        count += 1
    return count

nums = [2, 11, 10, 1, 3]
k = 10
print(min_operations(nums, k))  # Output: 3
import java.util.Arrays;

public class Main {
    public static int minOperations(int[] nums, int k) {
        Arrays.sort(nums);
        int count = 0;
        for (int num : nums) {
            if (num >= k) break;
            count++;
        }
        return count;
    }
    
    public static void main(String[] args) {
        int[] nums = {2, 11, 10, 1, 3};
        int k = 10;
        System.out.println(minOperations(nums, k)); // Output: 3
    }
}
#include <iostream>
#include <vector>
#include <algorithm>

int minOperations(std::vector<int>& nums, int k) {
    std::sort(nums.begin(), nums.end());
    int count = 0;
    for (int num : nums) {
        if (num >= k) break;
        count++;
    }
    return count;
}

int main() {
    std::vector<int> nums = {2, 11, 10, 1, 3};
    int k = 10;
    std::cout << minOperations(nums, k) << std::endl; // Output: 3
    return 0;
}
function minOperations(nums, k) {
    nums.sort((a, b) => a - b);
    let count = 0;
    for (let num of nums) {
        if (num >= k) break;
        count++;
    }
    return count;
}

let nums = [2, 11, 10, 1, 3];
let k = 10;
console.log(minOperations(nums, k)); // Output: 3
#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int minOperations(int* nums, int numsSize, int k) {
    qsort(nums, numsSize, sizeof(int), cmpfunc);
    int count = 0;
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] >= k) break;
        count++;
    }
    return count;
}

int main() {
    int nums[] = {2, 11, 10, 1, 3};
    int k = 10;
    int size = sizeof(nums)/sizeof(nums[0]);
    printf("%d\n", minOperations(nums, size, k)); // Output: 3
    return 0;
}
using System;

class Solution {
    public static int MinOperations(int[] nums, int k) {
        Array.Sort(nums);
        int count = 0;
        foreach (int num in nums) {
            if (num >= k) break;
            count++;
        }
        return count;
    }

    static void Main() {
        int[] nums = {2, 11, 10, 1, 3};
        int k = 10;
        Console.WriteLine(MinOperations(nums, k)); // Output: 3
    }
}

This Python code sorts the list and counts how many numbers are less than k. The loop stops as soon as a number meets or exceeds k, minimizing iterations.

Complexity

Time Complexity: O(n log n) for sorting.
Space Complexity: O(n) because Python sort generates a new sorted list (in-place sort would be O(1)).

Try this approach in the editor

Approach 2: Use a Min-Heap

Utilize a min-heap (priority queue) to repeatedly remove the smallest element from the array as long as it is below k. Increment a counter for each removal. This approach can be more efficient for larger datasets, although here n is small.

import heapq

def min_operations(nums, k):
    heapq.heapify(nums)
    count = 0
    while nums and nums[0] < k:
        heapq.heappop(nums)
        count += 1
    return count

nums = [2, 11, 10, 1, 3]
k = 10
print(min_operations(nums, k))  # Output: 3
import java.util.PriorityQueue;

public class Main {
    public static int minOperations(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int num : nums) {
            pq.offer(num);
        }
        int count = 0;
        while (!pq.isEmpty() && pq.peek() < k) {
            pq.poll();
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        int[] nums = {2, 11, 10, 1, 3};
        int k = 10;
        System.out.println(minOperations(nums, k)); // Output: 3
    }
}

Python's heapq module is used to create a min-heap from the list. Elements are popped from the heap until a condition is met, resulting in efficient minimum-value extraction.

Complexity

Time Complexity: O(n log n) for heap operations.
Space Complexity: O(n) due to heap storage.

Try this approach in the editor

Approach 3: Traversal and Counting

We only need to traverse the array once, counting the number of elements less than k.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

function minOperations(nums: number[], k: number): number {
    return nums.filter(x => x < k).length;
}
func minOperations(nums []int, k int) (ans int) {
	for _, x := range nums {
		if x < k {
			ans++
		}
	}
	return
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Sort and Count Approach

Time Complexity: O(n log n) for sorting.
Space Complexity: O(n) because Python sort generates a new sorted list (in-place sort would be O(1)).

Use a Min-Heap

Time Complexity: O(n log n) for heap operations.
Space Complexity: O(n) due to heap storage.

Traversal and Counting–

Complexity analysis

ApproachTimeSpaceWhen to Use
Sort and CountO(n log n)O(1)Simple implementation when modifying array order is fine
Min-HeapO(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

Frequently asked questions

Is Minimum Operations to Exceed Threshold Value I easy or hard?

Minimum Operations to Exceed Threshold Value I is classified as an Easy problem. The main skill tested is recognizing that every element below the threshold must be removed, which reduces the task to a straightforward counting operation.

Minimum Operations to Exceed Threshold Value I Python/Java solution

In Python or Java, the simplest implementation loops through the array and increments a counter whenever nums[i] < k. Heap-based versions can use Python's heapq or Java's PriorityQueue to repeatedly remove the smallest element while it remains below k.

How to solve Minimum Operations to Exceed Threshold Value I in O(n)?

Iterate through the array and maintain a counter for values less than k. Every element below the threshold must be removed, so the count directly equals the minimum number of operations. This approach avoids sorting or heap structures and uses constant extra space.

What is the best approach for Minimum Operations to Exceed Threshold Value I?

The most efficient approach is a simple linear scan of the array. Count how many elements are less than k because each such element must be removed. This solution runs in O(n) time and O(1) space and directly reflects the problem requirement.

Is Minimum Operations to Exceed Threshold Value I asked at Google/Amazon/Meta?

Problems involving threshold filtering, counting invalid elements, and basic array scanning are common in interviews at companies like Amazon and Google. While this exact problem may not always appear, the pattern of identifying and removing elements that violate a constraint frequently shows up in coding interviews.

What data structure is used in Minimum Operations to Exceed Threshold Value I?

The problem primarily uses arrays. Some implementations also use a min-heap (priority queue) to repeatedly access the smallest element and remove it until the threshold condition is satisfied.

What is the time complexity of Minimum Operations to Exceed Threshold Value I?

The optimal solution runs in O(n) time by iterating through the array once and counting elements smaller than k. Sorting-based approaches take O(n log n), and heap-based approaches also take O(n log n) due to repeated heap operations.

Comments

Now write it yourselfClose this page and solve Minimum Operations to Exceed Threshold Value I in the editor, without looking.
Practise Minimum Operations to Exceed Threshold Value I