Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3701_3800.s3759_count_elements_with_at_least_k_greater_values;

// #Medium #Array #Sorting #Binary_Search #Divide_and_Conquer #Quickselect #Senior
// #Weekly_Contest_478 #2026_04_25_Time_40_ms_(96.15%)_Space_134.18_MB_(73.08%)

import java.util.Arrays;

public class Solution {
public int countElements(int[] nums, int k) {
if (k == 0) {
return nums.length;
}
Arrays.sort(nums);
int i = nums.length - k;
int n = nums[i];
if (nums[0] == n) {
return 0;
}
while (n == nums[i]) {
i--;
}
return i + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
3759\. Count Elements With at Least K Greater Values

Medium

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

**Example 1:**

**Input:** nums = [3,1,2], k = 1

**Output:** 2

**Explanation:**

The elements 1 and 2 each have at least `k = 1` element greater than themselves.
No element is greater than 3. Therefore, the answer is 2.

**Example 2:**

**Input:** nums = [5,5,5], k = 2

**Output:** 0

**Explanation:**

Since all elements are equal to 5, no element is greater than the other. Therefore, the answer is 0.

**Constraints:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* `0 <= k < n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3701_3800.s3759_count_elements_with_at_least_k_greater_values;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countElements() {
assertThat(new Solution().countElements(new int[] {3, 1, 2}, 1), equalTo(2));
}

@Test
void countElements2() {
assertThat(new Solution().countElements(new int[] {5, 5, 5}, 2), equalTo(0));
}

@Test
void countElements3() {
assertThat(new Solution().countElements(new int[] {5, 5, 5}, 0), equalTo(3));
}
}
Loading