Find Kth Largest/Smallest Element in an Array | PriorityQueue in Java & C++ | DSA-One Course #33
Вставка
- Опубліковано 26 вер 2024
- Hey guys, In this video, we're going to learn about PriorityQueues in Java and in C++. We'll also solve one problem called : Find kth largest element in an array.
PriorityQueue implementation: www.geeksforge...
Code for Kth largest element: www.geeksforge...
🥳 Join our Telegram Community:
Telegram channel: telegram.me/re...
Telegram group: telegram.me/ds...
🚀 Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
📚 Complete DSA Playlist: • DSA-One Course - The C...
Complete Android Development Playlist: • Android Development Tu...
Hashtags:
#anujbhaiya #dsaone
Ignore these tags:
priority queue in java
priorityqueue in java
priorityqueue
java priorityqueue
what is priority queue in java
java priorityqueue in tamil
priorityqueue in java in tamil
priorityqueue class in java
priority queue in data structure
internal working of priorityqueue in java
java priority queue in tamil
priority queue in java in tamil
priorityqueue java
priority queue in collection in java
java priority queue example in tamil
priority queue in data structure java
kth largest element in an array
kth smallest element in an array
kth largest element in an array leetcode
find kth largest element in an array
kth smallest element
kth largest element
find kth largest elements in array
kth smallest element in an array in c
kth smallest element in an array in o(n)
kth smallest element in an array gfg
kth smallest element in an array using heap
kth smallest element in an array geeksforgeeks
In JAVA code
its,
pq.poll() ;
insted of
pq.pop();
10:04 In C++, to use min heap in priority queue, we use different syntax. If we define a priority queue as -->
priority_queue< datatype, vector, greater > pq --> this will create a min heap priority queue.
In case the datatype is int, it would look like -->
priority_queue< int, vector, greater >pq;
👇
That's so nice of you , I was finding this only..
we can multiply those numbers by -1 to use max heap as min heap
Priority queue is a abstract datatype. And heap is a data structure that makes the implementation of this data type very nice.
That conditional pop and push in the second loop was the game changer in the terms of time complexity
On fire 😅
First
Keep it up, daily videos upload karo bhaiyya
Bhaiya...to the point aap padha rhe... bahut hi aachese samaj aa rha :)
01:40 Priority Queue is a data structure where elements come and go on a priority basis.
03:20 Priority queue is a data structure used to find the Kth largest/smallest element
05:00 Implementing Max Heap and Min Heap in Priority Queue
06:40 PriorityQueue in Java & C++ implements min heap and max heap by default
08:20 The PriorityQueue data structure in Java can be used to find the Kth largest element in an array.
10:00 Find the third largest element using a min-heap in Priority Queue
11:40 Implementing a min heap priority queue to find the kth largest element in an array
13:15 To find the kth largest element in an array, implement a min-heap and replace the top element with the current element.
bhaiya daily videos laao bhut help hoti aap ki videos se.
Hats off to you Bro
Thanks a lot bhaiya❤..U r our lifeline✨❤❤
I was stucked on the same question , thanks bhaiya 👍☺️
#include
#include
using namespace std;
int main()
{
int n=5;
int arr[n]={3,7,8,6,5};
int k=3;
sort(arr,arr+n);
cout
@@Pritamdaspk It won't work if the array has duplicates
Love You Anuj Bhai I from Pakistan. What a beautiful way of teaching.
It really helped me to understand the concept, I was not able to solve this question from very long time..
@@kartiksharma2037 Abey saale
@@kartiksharma2037 khali chori pata ta hai LOL
gaurav
Awesome explanation
Thankyou so much
It helps me to proceed further
Carry on like this and help us ❤️
The time complexity for this solution is O (N Log K) and space will O(K).
We have to visit every element of given array hence the O(N) and for inserting into a PriorityQueue it takes O(N) but here the max element in PQ at any moment is K the final time becomes O(N Log K).
We are using a PriorityQueue whose size cannot exceed K. Thus the space complexity becomes O(K);
for inserting into a PriorityQueue it takes O(logN)
@@kapilsingh2816 extract krne mein O (log n ) and ye 'k' times lag rha hai toh O (k log(n)) hojayega ig..
very good explanation, pls provide time and space complexity after every video.
Thank you so much Bhaiya..😇
@Harsh Rajput 😂😂😂😂
bhai kya smjaya mzza a gya 🤗
Bs ase hii video ki frequency chye❤️
💯💯
Hey, Anuj Please make a video on any one of them it's your fan request/viewer 1. How to become a Full-stack Software Developer,
2. How to become RPA Developer
3. RoadMap to career path In Devops Engineering detail. salary, scope, demand,
thank you bhaiya ❤️ for this amazing lacture 🔥🙏
best video for priorityQueue thank you sir
Thank you so much Sir..😘😘😘😘😘😘😘😘😘😘🥰
Very well explained.
Anuj Bhaiya Ji Namaskar from sangam city.
Sir mere ko programming Nahi aati.
😊😊
bhaiya "Merge k Sorted Arrays" question kra do pl
coding me pure youtube par ek apk samjhane ka tareeka hi mast hai
keep going!!!!!! love the video
We Also make maxHeap . Make Loop of K-1 and pop element . after loop return top() element
Bhaiya is Algorithm ki time complexity O(n) hogi kya?
Or agar is question ko Arrays.sort() method se karenge to time complexity O(nlogn) hogi?
what is the time and space complexity of this solution?
Explanation 500x better than college professors
anuj but largest ke liye gfg me or another platforms pr max heap apply hai and smallest element ke liye minheap , just opposite your method.
Bhaiya please 1 video bana dijiye development ke liye Android Development seekhe ya Phir Spring Boot seekh k backend/Web Development kre...
Please!!!
If I'm not wrong @5:15 in java minheap's structure 15-shoud be in left of 5, It inserted before 10...
If wrong someone correct me..
Thx in advance
Your videos are amazing please try to make videos in English.
Bhaiya mere method time complexity jyada hosaktha hai but yeh approach bhi sahi hoga na java mai
Hum sab element priority queue mai daal dege and comparator pass kardege reverse order ka and we will pop that many many till as the value of k and jaise he reach karege waise woh print kardege
bro aap phle code me pop () use krr rhe hain aur kth largest me poll () dono work krta h kya max/min return krne k liye
Thank you so much bhaiya for this awesome video.
PriorityQueue doesn't have pop() method. So it should be pq.poll() in code.
good tutorial helped me a lot ....Thanks Sir Ji !!
Nice video bhaiya
bht achha samjhayein
Sir, how to start preparation for software companies ....currently in 1st year bsc computer science
Is there any hope for getting a good job in software companies just after passing bsc comp sc as a fresher?
No
Yes bro i am in
Yes, there is hope and probability as well.
Thanks
I request everyone who's following the playlist, please like the videos as this motivate the creator a lot. Bhaiya is doing such an amazing job by providing us premium content for free. Support him and also share the playlist with needy people.
can we heapify that array and extract k element from that array?
Please graph and dp ka problems bhi upload karna because UA-cam me accha contents nhi mile🙏
Anuj Bhaiya love you broo.
Awesome bhaiya
Sir I want to become an Software developement Engineer so what skills required for it..I mean what strategy I have used,like subjects and all...
Bhaiya please make a video on Freelancing also..... Like Web development.... We Youngsters want to generate some initial income after High school .... So, please make a detailed video on that....❤️❤️
please also explain this with quickselect algo
time cmplexity of pq.push() ?
Great video bhaiya
Anuj bhai like kriye
Bhiya dheere dheere karke linked List bhi upload karna
In c language protity queue have or not
dhanyawad
Underrated video
Can you make a live video of shuffler app please. I am stuck. In android development tutorial playlist.
great explanation
Wow superb
Nice Expalined
Agar elements ko sort kar liya hai to heap kyo banaya........Kth largest chahiye to (K-1)th element lelena
nice explanation bhaiya
but priority queue is min heap by default right?
Thanks a lot 👍👍👍 Sir
Bhaiya please upload video on regular basis ......... please
Bhaiya time and space complexity kya hogi iski?
Bhaiya according to ur estimation in how many videos will DSA and algorithm course finish??and will u start a C++ series from scratch to advanced level for placement specific after which we will be able to crack all the questions in C++ language, after the DSA series🥺🥺🥺❤️❤️because i only get the feel of coding from ur channel. Hope u will do it too and help me achieve my dream package from a Tier -3 college😔🥺❤️🤗🔥
💯👍
Maza aa gaya 🔥🔥
Does it help in case of duplicate elements?
yes, heap allows duplicate elements
thanks bhaiya
Why priority queue method is greater then sorting algo nlogn
bhaiya IDE me kiya kariye na doubts reh jaate hai bhut :(
Can you male a live video of shuffler app please. I am stuck.
What is TC here? is it O(n)+O(klogk)?
I did as you said but It doesn't work. As I know It should work but I got TLE using priorityQueue
11:03 if you are coding in Java should be pq.poll()
what is the time complexity of this approach?
isn't it O(nlogn)🤔....and the question says it should be done in O(n) TC.
Slight confusion i have, if we need to solve this using a priority queue to get k=3rd largest number, then
Why can't we just insert all elements in a max heap , and iterate over it till 3rd element is there and return it? in case of duplicates also , we can compare it with side elements, or insert it in a set. can someone explain?
But in your case, the space complexity will be O(n). Here the space complexity is O(k) which is most optimized.
@@sohamroy9272 thanks
@@sohamroy9272 anyway we are iterating through whole array, so its O(n) only
@@prtdesh25 I am talking about the SPACE complexity which is O(k) and time complexity is O(n) because we are iterating the whole array.
bhai pehle cpp wale code mein hi error aagya... kya likha hai
to0
easy sir thanks
pq.poll(); to remove top element in PQ
can't we directly return 3rd last element of the sorted array??
during sorting it will take nlogn while here it will be n+klogn
🔥🔥
clearly explained
Question at 7:37
7:35 Question - Print K-th Largest element in an array
why smallest element from last three element should be our Kth largest element and not from first three elements?
pop k jagha poll se bhi kaam chal jaa rha hai
Imagine getting a Heart From Anuj bhaiya
Bhaiya aapne ab tak kitne internship kiye hai?
1
Abhi aap kya kar rahe ho
Bhaiyan Intuition toh bana hi nhi, 300 questions mein iska code kaise yaad rakho ?😢😢
placement season chalu hoga gya hai sir, please jaldi sab videos daaldo
1st like ❤️
🙌🙌🙌
what if there are duplicates in an array