LeetCode Entry

3375. Minimum Operations to Make Array Values Equal to K

09.04.2025 easy 2025 kotlin rust

Count number bigger than k

3375. Minimum Operations to Make Array Values Equal to K easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/953

Problem TLDR

Count number bigger than k #easy

Intuition

The problem description is the hardest part. The brute-force simulation would be like this: remove largest, skip duplicates, repeat.

Approach

  • we can use a counting array or a bitset

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code


    fun minOperations(n: IntArray, k: Int) =
    if (n.min() < k) -1 else n.toSet().count { it > k }


    pub fn min_operations(n: Vec<i32>, k: i32) -> i32 {
        let mut s = [0; 101];
        for x in n {
            if x < k { return -1 }
            if x > k { s[x as usize] = 1 }
        } s.iter().sum()
    }


    int minOperations(vector<int>& n, int k) {
        bitset<101> b;
        for (int x: n) if (x < k) return -1; else b[x] = b[x] | x > k;
        return b.count();
    }