 What's up guys, my name is Michael and welcome to my YouTube channel. Today we're going to talk about this Leak code question find the kth largest element in an array So you might be wondering How do I oh whoops? How do I find the kth largest element array? So what does that mean? So this is a question find kth largest element in array Or in anything basically you're trying to find the kth largest element in array So what does that mean? well Let's say I have an array of some random numbers. Let's say Let's say I'll just use this example four five eight two three Five ten Whoops, I need more space nine and four So I have these values in my array The kth largest element Would be like find the first largest number so like let's say if k equals one that means I want to find the first largest number There's a largest number If k equals the two that means I didn't want to find the second largest and so on right If I want to find if k equals one that means I want to find the first largest k equals two I once find the second largest number, okay So you might be wondering well Have this array What's the most basic way to solve this question? Easiest way is to sort the array first now if I were to sort this array Which would be let's see If I were to sort this array I would get These numbers Okay, so like let's say two Two is gone. So I'm gonna put a check mark when it's done three Sort this for four Four there's another four here five There's another five here. Yeah, five here five. What was else eight nine and then ten? Yeah, so eight nine and Then eight nine and then ten so if I were to sort these arrays sort the array Okay, the largest element so For one the first large it would always be at the end of the array, right? So if k equals one first largest element would be ten Right, so this would be if k equals a one right First largest is ten. It would be always at the n array What about if k equals a two? What is the second largest second large number would be nine? Second largest would be nine Third largest Would be eight and so on and so on okay Now what is the problem with the solution? Problem is that if I have an array that is this size Basically I would have to sort the array first Then I had to have to iterate from the end I have to iterate the values from the end until I get to whatever the kth value is right So like if like I showed you already First largest is ten second largest nine third largest eight so now You might be asking how do I solve this then how am I supposed to solve this problem if I only How do I make this problem more efficient, right? Like if I can't just build Like like sort the array every single time and then iterate from the end I mean you could but that takes time complexity of O of n and log n Just sort assuming that this time complexity assuming oh whoops Assuming this time complexity I need to move this upward here assuming this time complexity is a quick sort That would get you You have to do this every single time and then after that you have to Iterate from the end and that would be another O of n O of n time complexity don't iterate from then So how do I do this question? well another way to do this problem is to think about how Just to use something called a heap Now a heap is basically Depending on the type of the heap It's basically an array Sorted already except you only have the axis to the first value. Okay, so if it's a min heap That means that all the values in the array or in the whatever how many all the values are sorted from Decreasing order increasing order. Yeah, min heap That means that the first value would be the smallest the second one would be the second smallest third will be third smallest yada yada yada He's a max heap that means the first value would be the largest element second would be the Second largest third element would be the third largest. So now, how do you do this with a heap? okay, so What you could do With a max heap basically like I told you already Heaps are basically sorted arrays except you only have access to the top element So in this example here Four five eight nine two three five ten nine four If I were to build a heap of All these elements right With a max heap, this is gonna be a max heap I'm not gonna go into how like heapify works and how all the Elements of how heaps maintain their structures, but I'll just show you what it would look like if you were to build a max heap of Let's see one two three four five six seven eight nine nine elements, right? What's gonna happen is that all these elements? Are exactly gonna be Build a max heap the largest elements would be first. So this is gonna be the same thing except it's gonna decrease in Decreasing order. So this would be ten Let's see nine. I could just look at the Nine eight five five four four Five five four four three two Okay This is actually not how it's like structured in like the that's actually not how a heap would work look like But I'm just showing you like like how it's like internally how it's structured It's not actually how like a heap would look like heaps use Things are like binary search trees like the way that it looks like but this is just how like I'll just show you guys How it would work if you're using a max heap? So if you were to build the same array using a max heap And you want to find the kth largest Kth largest right if k equals the one That's the first largest that's just gonna be ten Okay, so that would give us the value of ten What if I want to find the second largest Well, I only have access to the first element of the heap So that means I have to pop it. I have to remove the first element from the heap. So that means I have to remove ten and look at The top most element which would be nine. So then in this case K if the second largest element would be nine So you can see the issues with this the problem is that you would have to go through if Based on your previous array If the size is end or like let's say the size is equal to n in this case So nine nine elements you have to build a max heap of nine elements and then you would have to iterate Not iterated. Well, yeah, you basically had it right but you have to Keep removing the first element once you finish building your heap And then you have to get the first element every time and then that basically is going to be Your answer. So that's the issue with a max heap. So what is the best way to do this instead of using a max heap? Well, you have to use a min heap. Now, you might be wondering how am I going to do this with a min heap? Min heaps are sorted smallest to largest, right? smallest to largest you and you only have access to To the first value of the heap which means that you only have access to smallest elements of your data Okay, so I'm going to get another sheet of paper So back at our example again We're gonna use a Yeah, okay, we're gonna use a min heap Min heap way, okay Okay, so this is our data again array This is this is what was given to us. Okay, this is the I wrote many way. This is not the main heap, but four five eight two three five Ten nine and four okay so How do I do this with a min heap? What you're gonna do is you are going to build a min heap of size K so K is gonna be So remember we're given we have to find the kth largest element, right? If I want to find I'm gonna build So kth largest like Remember I told you already First if cake was one first largest cake was two second largest cake with three third largest I'm gonna build a min heap of size K. So in this case, it's going to be Let's say I want cake with a three That means I'm gonna have so I want to find third largest element right That is going to be K equal to three. So I'm gonna have three elements here and In a min heap remember the smallest m elements are first and it goes smallest to largest Right so I'm gonna iterate through my array and I'm going to add The values all into my heap. So four five eight. It's already sorted, right? It's already sorted from smallest to largest. So it would be four five eight This is just an example of how many forces. It's not actually like a does our current in heap It's actually this is not how it's structured in memory by the way But I'm just showing you guys if you were to use them in heap. This is what it would look like Okay, it's a like actually they would have like nodes pointing to left to right child But just for simple simplicity sake. I made it like this. Okay, okay? So now We're building a min heap four five eight Yeah, what am I gonna do I? Am basically going to Check the first element of my min heap, which is four while I'm iterating through it through the array If whatever value is larger than four, it's the current value that I'm iterating through the array is Larger than my min heap. I'm gonna remove the first value of my min heap and that's gonna reorder everything Okay, I'm gonna keep doing that until I get to the end of the rate So in this case we already added the first first K First K Values into the array. So I'm gonna start out here at two is Two greater than four. No So I'm not going to add or remove the first value of my human heat So now is three greater than four. No, so I'm not going to add the first value of my name in heat Okay, it's five greater than four. Yes. So what am I gonna do in my? Heap I'm gonna remove the fourth value and I'm gonna add the fifth value and then In the internals of the heap It's basically gonna sort it for you You we are we are not going to go over how heap of fire works how that all the internals of a heap works But basically it would get you the sorted values and you would have five five eight I'm at I added five after removed four. So I have five five eight Now we're about ten Ten is greater than five. Okay. I'm gonna remove five and I'm going to add ten to my heap So now that will after all the heap of five works five eight ten. Okay Okay Well by nine nine is greater than five. So I'm going to Now it's greater than five I'm gonna remove the first value of five and I'm gonna add nine to my heap. So now it's gonna be eight nine ten and That's going to after all the heap of fireworks. It makes it in sorted order from smallest largest a nine ten Then I'm gonna add four And I'm not gonna have four. I'm not gonna add four because four is not greater than eight Four is not greater than eight. So yeah, I'm not gonna add it So now my map my heap in the end my in heat me hit men heap in the end Is going to be eight nine ten So if I wanted to find remember kth largest To the third largest element is just end up is it going to be eight the first value of our men heap Which is gonna be eight. So if k equals a three The third largest is Going to be the first value of our men heap, which is gonna be eight and as you can see here. Oh, whoops As you can see here, that means this eight is exactly the same thing as If we sorted our array and iterated from the end k equals three third largest is eight So that's how you would find the kth largest element in array with a men heap. You would pretty much Maintain your men heap to you'd have a men heap of size k and then you would maintain it and If while you're iterating through your array if it's larger than the first value the top most value of our heap we're going to Remove the top most value of our heap and then we're going to add the value into our menu. Okay So that's basically how you would do it. I explained guys how to do this question now we're gonna go and actually code it in the The problem so the problem was basically kth largest element of stream but that's the basically the same thing as the problem kth largest element in array so now we're going to go ahead and code it in the in the I'm gonna move to the computer and I'm gonna code this with you guys. Oh Okay, I'm gonna create a priority queue Which is a heap? It's gonna be integer. I'm gonna pass in a vector It would be to do greater And I'll call this me heat Okay, then I'm going to go through my elements in my array and what I'm going to do is I'm going to do If my size of my men heat is Less than k a k Which is the size I'm building k is the size that I'm building right if my Heapsize lesson k, then I'll just add it to my main heap. Okay now else if The value I'm adding is greater than the men heaps Men heaps top top most value Value I'm adding is greater than the men heaps top most value then I'm going to basically I'm going to basically do is I'm going to remove the men heat remove the top value of the men heap and then I'm going to add the value Push men thumbs at Okay, and in the end after this for loop I'm going to return men heap top submit it and Got accepted faster than 97.7 8% So that's how you do this question raycom subscribe. I'll check you guys later