Kth largest element in an array | Kth smallest element in an array

Kth largest element in an array | Kth smallest element in an array

Techdose

5 лет назад

236,809 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ajay Gautam
Ajay Gautam - 31.10.2023 15:26

I wish the explanation included details on max and minheaps...

Ответить
chandra hans prakash saha
chandra hans prakash saha - 11.08.2023 21:19

java solution:
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());
for(int i:stones){
pq.add(i);
}
while(pq.size()>1){
int f=pq.poll();
int s=pq.poll();
int diff=Math.abs(f-s);
if(diff>=0){
pq.add(diff);
}
}
return pq.poll();
}
}

Ответить
ibuyk x
ibuyk x - 05.03.2023 13:05

I have a question what if we use bubble sort. But in our program will be like this

Pseudocode #

Store first array elem : int x = arr[0];
Now loop till n-1
Inside the loop check if arr[n] >= x;

Do we get O(n)?
Which is better that O(nlogn)

Ответить
soumyajit mishra
soumyajit mishra - 06.01.2023 08:36

I think insertion in a heap also takes nlogn time

Ответить
Sonu Sah
Sonu Sah - 04.11.2022 20:10

Thank you for the lecture

Ответить
Amey Gadhe
Amey Gadhe - 20.09.2022 06:16

Hey brother cant we just sort the array and then giv the index for example
we can use sort(arr,arr+n);
and then return arr[k-1]

Ответить
Gourav Kr Roy
Gourav Kr Roy - 10.09.2022 15:53

does building heap takes o(n) time?..i think its o(nlogn)!

Ответить
Saikiran K
Saikiran K - 11.07.2022 12:43

We can also use priority queue (maximum) such that will pop elements upto k-1 and simply print the top

Ответить
Akash Jaiswal
Akash Jaiswal - 26.06.2022 15:29

The first approch doesn't work if there's duplicate values in array.

Ответить
Prashant Singh
Prashant Singh - 28.05.2022 21:50

how is buildig a heap takes n? i mean should not it be O(n * logn)? i.e.. overall complexity of this approach should be O(n * log n + k * log n) = O((n+k)log n)

Ответить
RITUL BHATNAGAR
RITUL BHATNAGAR - 08.04.2022 10:04

easy c++ solution using priority queue
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int>pq(nums.begin(), nums.end());


int ans;


for(int i=1; i<=k; i++){
int a = pq.top();
pq.pop();
if(i==k){
ans = a;
}
}

return ans;
}
};

Ответить
Akshay Bhanderi
Akshay Bhanderi - 22.02.2022 06:55

building heap is not O(N), its Nlog(N).
hence TC will be nlogn + klogn

Ответить
Saunak Nandi
Saunak Nandi - 21.02.2022 06:17

How the avg. time is O(n) for Quick select ? It should be O(kN)

Ответить
ZeeshanCanDo-AnythingHalal
ZeeshanCanDo-AnythingHalal - 09.02.2022 21:37

What if there is duplicate element in an array

Ответить
cinema ante pichi
cinema ante pichi - 09.10.2021 17:46

using max heap or min heap you get NlogK time complexity

Ответить
Muhammad Waseem
Muhammad Waseem - 13.09.2021 22:51

Well you can use Bouble sort to get nth largest no. Because as we know that on each iteration bouble sort sorts the largest no to the most right and others sort before it. So we have to iterate nth time and then at position array.length - nth_largest_ we have the desired no and this algo will give us nth_no*n which will be considered n and constant will droped. And for nths smallest no we can we same logic

Ответить
Niladri Paul
Niladri Paul - 01.09.2021 22:15

Can we use a set for this? Set has time complexity of logn, isn't it?

Ответить