4141. Count Elements With at Least K Greater Values
My accepted Python solution to LeetCode problem 4141, Count Elements With at Least K Greater Values, running in 1463ms.
- Difficulty: Medium
- Python
- Runtime 1463ms
- Memory 30.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1463ms, memory 30.3MB, accepted 2025-12-30.
class Solution:
def countElements(self, nums: List[int], k: int) -> int:
n = len(nums)
sorted_nums = sorted(nums)
count = 0
for num in nums:
# Count how many elements are strictly greater than num
# Using binary search to find first element > num
# Then count = n - that_position
left, right = 0, n
while left < right:
mid = (left + right) // 2
if sorted_nums[mid] <= num:
left = mid + 1
else:
right = mid
greater_count = n - left
if greater_count >= k:
count += 1
return count