Priority Queue Kth Largest
Published at25 Jul 2026
Created by M Ichsanul Fadhil
Find the kth largest element in an array (simulating a priority queue). Sort descending and return the kth element. Return -1 if k exceeds array length.
Example Input & Output
Example 1
Input
[1],2
Output
-1
Explanation
Not enough elements
Example 2
Input
[10,5,7],1
Output
10
Explanation
1st largest is max
Example 3
Input
[3,1,4,1,5],2
Output
4
Explanation
2nd largest is 4
Example 4
Input
[5],1
Output
5
Explanation
Single
Example 5
Input
[1,2,3],3
Output
1
Explanation
3rd largest is min
Algorithm Flow

Recommendation Algorithm Flow for Priority Queue Kth Largest
Solution Approach
Best Answers
java
import java.util.*;
class Solution {
public int solution(int[] nums, int k) {
if(k>nums.length)return -1;int[] s=nums.clone();Arrays.sort(s);return s[s.length-k];
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
