[LeetCode] Count Elements With at Least K Greater Values

3759. Count Elements With at Least K Greater Values

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countElements(vector<int>& nums, int k) {
map<int,int> freq;
for(auto& n : nums) freq[n]++;
int res = 0, tot = nums.size();
for(auto& [_,v] : freq) {
tot -= v;
if(tot >= k) res += v;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/count-elements-with-at-least-k-greater-values/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.