LeetCode 215. Kth Largest Element in an Array [Algorithm + Code Explained ]

preview_player
Показать описание
One of the most frequently asked coding interview questions on Arrays in companies like Google, Facebook, Amazon, LinkedIn, Microsoft, Uber, Apple, Adobe etc.

LeetCode : Kth Largest Element in an Array

Question - Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.

Рекомендации по теме
Комментарии
Автор

Hi Jayati, Thank you for the video, it was helpful, would you mind sharing the ppt you are using for various approaches we can use to solve DS and Algo?

atharvapuranik
Автор

What if the new element is in between both nodes in the min heap? Do we put that node in the middle? So does that mean we check both values first?

omarkhan
Автор

There's a optimized soln using quick sort.Please make a video on that

akshatjain
Автор

Can you please post the solving methods ppt as well.

ramaseshannarasimha
Автор

mam please make playlists on most important topics on Datastructute.

rajkumarchaudhary
Автор

Can anyone please explain why it is nlogk?
We are inserting every element into priority after checking the condition so it should be nlogn

tradingjournal
Автор

You may need to use digital pen for better explanation just a suggestion

piyushsoni
Автор

mam please give the access of that ppt u r showing in videos . It will be very helpful for me.

surajgrandhi
Автор

Quickselect is a much cooler way to solve this problem.

jlecampana
Автор

import java.util.*;
public class Solution {

public static int kthLargest(int arr[], int k) {
/* Your class should be named Solution
* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
PriorityQueue<Integer> pq = new
for (int i=0;i<arr.length;i++){
pq.add(arr[i]);
}
int output=0;
for (int i=1;i<=k;i++){
output=pq.remove();
if (i==k){
return output;
}
}
return output;


}
}



try this code . its much better than yours

jatinbhatoya