Skip to main content
Back to problems
#3759
Medium Algorithms

Count elements with at least k greater values

Array Binary Search Divide and Conquer Sorting Quickselect
31.2% acceptance
Feb 25, 2026
70
7
You are given an integer array nums of length n and an integer k. An element in nums is said to be qualified if there exist at least k elements in the array that are strictly greater than it. Return an integer denoting the total number of qualified elements in nums.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_elements(nums: Vec<i32>, k: i32) -> i32 {
    if k == 0 { return nums.len() as i32; }
    let mut sorted = nums.clone();
    sorted.sort();
    let n = sorted.len();
    let k = k as usize;
    let threshold = sorted[n - k];
    sorted.iter().filter(|&&x| x < threshold).count() as i32
  }
}