Skip to content

Commit 2639fae

Browse files
authored
Added task 3759
1 parent 6b74c45 commit 2639fae

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3701_3800.s3759_count_elements_with_at_least_k_greater_values;
2+
3+
// #Medium #Array #Sorting #Binary_Search #Divide_and_Conquer #Quickselect #Senior
4+
// #Weekly_Contest_478 #2026_04_25_Time_40_ms_(96.15%)_Space_134.18_MB_(73.08%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int countElements(int[] nums, int k) {
10+
if (k == 0) {
11+
return nums.length;
12+
}
13+
Arrays.sort(nums);
14+
int i = nums.length - k;
15+
int n = nums[i];
16+
if (nums[0] == n) {
17+
return 0;
18+
}
19+
while (n == nums[i]) {
20+
i--;
21+
}
22+
return i + 1;
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3759\. Count Elements With at Least K Greater Values
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n` and an integer `k`.
6+
7+
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.
8+
9+
Return an integer denoting the total number of qualified elements in `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,1,2], k = 1
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
The elements 1 and 2 each have at least `k = 1` element greater than themselves.
20+
No element is greater than 3. Therefore, the answer is 2.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [5,5,5], k = 2
25+
26+
**Output:** 0
27+
28+
**Explanation:**
29+
30+
Since all elements are equal to 5, no element is greater than the other. Therefore, the answer is 0.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
35+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
36+
* `0 <= k < n`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3759_count_elements_with_at_least_k_greater_values;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countElements() {
11+
assertThat(new Solution().countElements(new int[] {3, 1, 2}, 1), equalTo(2));
12+
}
13+
14+
@Test
15+
void countElements2() {
16+
assertThat(new Solution().countElements(new int[] {5, 5, 5}, 2), equalTo(0));
17+
}
18+
19+
@Test
20+
void countElements3() {
21+
assertThat(new Solution().countElements(new int[] {5, 5, 5}, 0), equalTo(3));
22+
}
23+
}

0 commit comments

Comments
 (0)