Given an integer array nums and an integer k, return the largest k elements from the array in any order.
An O(n log k) solution is expected.
Complete the function topKUsingPriorityQueue in the editor below.
topKUsingPriorityQueue has the following parameters:
int[] nums: the input arrayint k: the number of largest elements to return
Returns
int[]: any ordering of the largest k values.
Examples
01 · Example 1
nums = [3, 2, 1, 5, 6, 4] k = 2 return = [5, 6]
The two largest values are 5 and 6. Any order is acceptable.
Constraints
1 <= nums.length <= 2 * 10^5-10^9 <= nums[i] <= 10^91 <= k <= nums.length
More Uber problems
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026
- First Unique IP Hitting the ServerPHONE SCREEN · Seen May 2026
public int[] topKUsingPriorityQueue(int[] nums, int k) {
// write your code here
}
nums[3, 2, 1, 5, 6, 4]
k2
expected[5, 6]
sign in to submit