 What's up guys, Mike the Coder here. Today we are going to talk about two heaps, which is another coding interview pattern. I know I've said I was going to do the two new series today, but I feel like we should finish this series first and then we could be on our way. And yeah, let's just try to finish this series first. So we're gonna talk about two heaps. And the gist of two heaps is actually not that difficult. It's basically we're going to maintain two heaps. One is gonna be a max heap and one is gonna be a min heap. Okay, so let's actually give out an example. Let's say we are trying to find the median of an array or something, a data stream or an array. This is generally when speaking, what happens a lot in coding interviews, they give you like an arbitrary number of elements at yada yada, but this is this pattern. Well, you could use it on most interviews. You would have to use a heap somehow sooner or later. You'll probably have to use a heap. Guys, don't remember how medians are. So let's say I give you an array of numbers like this, right? An array of numbers, one, two, three, four, five. The median will be the middle number. It'll be the middle number here. So if I were to cross a value from the first one and then the last one and the second one, the last one, the last second or the last one from the beginning and then cross the back one, the middle number would be three. So this median would be three, okay? Now the only time when the median is not this is if we have an even length of numbers. So let's say we have one, two, three, four, five, six, right? In this case, if I were to repeatedly cross out the beginning and the last number, see the beginning, second one, last number, we have two values here, three and four. So the median in this case will be the sum of them divided by two, right? It'll be the average of them. So it will be three plus four and then we divide it by two which will give us 3.5, okay? So that would be the median of this, okay? So what is the easiest way to do this? Well, if you have like in competitive programming, the easiest way would just be to sort them and then just get the middle value, right? Because if you sort the values and then you have the length of the array, you could just get the middle value. And if it's even, you could just like add the middle value, the middle value of this one and that one divided by two, right? But we can't do that in this coding interview because for some reason, we can't store all the numbers together and we can't just store every value we see because it just takes too much storage. So before we get started, some of you guys don't know what heaps are. So let's actually go over what a heap is. So a heap is a complete tree. One, there's two properties. One, it is a complete tree. Two, it has either the minimum value is on the top of the root or the maximum value is at the top of the root. So this would be example of a complete tree. So let's actually go over what that is. So one, it is a complete tree. So it has to be a complete tree too. The root must be a min or max, okay? So what is a complete tree? So let's say I have values like this. One, two, three, four, five, six, seven, okay? This is a complete tree. Why? Because all the values have been filled up before the next row and all the values have been filled up from left to right, okay? So let's see what's not a complete tree. This is not a complete tree. Not a complete tree. Why? Because this six should have been on this side. This six should have been here. Remember, all the values have to be filled up from left to right. And if you think about it, my old CS teacher would have been like, they're like parking lots, okay? You have to fill up the values between the left and right until they're all completed, okay? So we cannot have random values like this. This is not a complete tree, okay? All right, what is another thing about a heap? What is also not a heap, okay? This is also not a heap, right? Because not a heap because this six should have been on this side, the left side. The values have to be filled up before the next level. All the values have to be filled up until the next level. So this six should have been over here, okay? They have to be filled up. They're like parking lots. All these nodes from left to right. This has to be filled up. This has to be filled up. This has to be filled up. This has to be filled up, right? This six, then this has to be filled up. Then this, then this, okay? They have to be filled up before the next one. What else is not a heap? This is not a complete tree. This is not a complete tree. Why? Because this four should be on the right side. Each level has to be filled up before the next, okay? This four has to be in this side. They have to be filled up, okay? They have to be filled up, okay? Likewise, this is also not a complete tree. We have a four and then a two here. Not a complete tree. This four should be on this side, okay? I'm just saying, okay? And what else? The second property is the root has to be in the min or max. So, out of these values in this tree, this is a min heap. Why? Because this root is the smallest value of all these values. One is smaller than two, one is smaller than three, one is smaller than four, okay? The root is the smallest value, okay? What is the other property? It could also be the largest value. So if I have six and then we have one, two, three, four. Yes, this is a max heap. Why? Because this root is the largest value. It's the largest value of this heap, okay? Largest value. What do you mean by six? This root is the largest value. That's why we consider it, okay? So that's what makes this a heap, okay? Also, it's because it's a complete tree. All the values are filled up from left to right. They're all filled up in this tree. Okay, so now that I got you the gist of the theory behind heaps, how do you do this problem? So remember, we need to get the median of values in this array, okay? So what are we gonna do? What we're going to do is we are going to have a max heap for all the values that are smaller than the middle and that half of the array and a min heap for all the values that are larger, the largest for the larger half of the array, okay? So to do that, let's have, let's think about this. We're going to, let's consider adding values here, okay? So this one, this left side would be max heap and this right side would be min heap. So let's start from left to the right side. I'm gonna add one to my heap, okay? I'm gonna add one to my heap. So one is gonna be in here, okay? Then I'm gonna add two. Remember, this is a max heap. So this two becomes here, goes here, right? It becomes a max. So this two would actually be right here. It reorganizes itself to be right here, okay? I'm gonna add three. It's gonna reorganize itself. So when I add three, it'll be three, one, two, okay? Bam, that's our max heap here. And the right side, four, five, six. Let's add four, four will be the min heap, right? Four, add five, bam, add six, bam. So now that we add all our values, what do we do? Well, since we have the maximum value on the smallest end of the sides on this side, the maximum value, the smallest end of the side, we have the three. So we could just take the root of three, which would be three. And then if we want the smallest value, smallest value for the right side of min heap, the root will be four, right? Four, so we could just add these up. Three plus four divided by two will give you 3.5, okay? So bam, that's the fastest way to do this, okay? That's the fastest way, okay? All right, so what if the array of numbers is odd, right? If the array of numbers is odd? Well, the median would be three, right? The median would be three. So in this case, our max heap should have one more than the min heap. So in this case, our max heap, let's say I add one here, right, and then I add two and then I add three like that. And then our min heap would have four and then five, okay? So in this case, we need to return the middle value and it would just be the root of the max heap. So it would just be three, okay? So if we have an odd number of values and they're odd, right? We'll just return the root of the max heap, okay? So in our situation, we're always gonna have one more element for the max heap. The max heap will always have one more element, okay? I'm just making sure you guys understand this. Max heap will always have one more element. So in the case when it's odd, right? We'll just return the first value of the max heap, okay? Okay, so how do we make sure that the max heap always has one more element? We use a way called balancing. So I'll show you guys what that means. All right, guys, so how do we make sure that we balance it properly? So the simplest way to do it is every time we add numbers to both our max heap and min heap, if the value on the left side of our max heap is smaller than the value on the right side of our min heap, we're just gonna remove the top node of this value and then we'll just add it to the max heap. So we'll move the top node and then add one more to the max heap. That's assuming that the number of values in the max heap is smaller than the min heap, right? So we'll just remove that and put it there. And then we'll do that every single time when reorganizing when we have the max that is smaller than the number of values in the min heap, okay? So that's basically what reorganizing will be. So I'll show you guys the code of that right now. So hopefully you guys can see this. Can you guys see this? Do, do, do, do. Bam. Okay. So this is what the code looks like, okay? This add number is a reorganizing, okay? Low will represent the max heap and then high will represent the min heap, okay? So they just represented this this way. So let's actually go over an example of how this works. While we're adding the values to make sure it's getting reorganized, okay? So our min heap and max heap and min heap, okay? So, okay. We start the value first, a one, we push it to low. So low gets pushed here. High will also get pushed with value one. This is just making sure that low is top is one. So we're making sure both of them have the same number currently. And then what we're gonna do is we're gonna pop low. So low is gonna, we're gonna remove this. It's gonna be gone, okay? Now, since low is smaller than high, so the number of elements here in the max heap is smaller than the number of elements in the min heap, we're gonna push whatever values, we're gonna remove the first value on this root and put it in here. So we're gonna take this and we'll put it in here and we're gonna remove it. So we're gonna do that is basically, we push one, the root that was on here and then we remove it. So now it's gone, okay? So that was one. Now let's go to two. So right now, currently we just have, currently we just have, currently is one here, right? Because after all the removing, this is what we have. All right, so now we have two. What are we gonna do? So low is gonna push two, so two is gonna get pushed onto low, so it looks like that. And then high is gonna get pushed low's top. So what is the top, what is our root of our max heap? It's two, so our min, we're gonna add two here. Also, we're gonna pop the low, so then this is gonna be gone. So this is gonna be gone. Two is gonna be gone. All right, is low smaller than, is the size of the max smaller than the size of the min heap? Nope, so then that's that. We do that and then we'll go to the next value three, so let's actually get rid of that. So now our values will have one and two. Right, and then we're done with this. We're gonna go with three. Okay, what are we gonna do? We're gonna push three onto low, so three goes here, okay? High is gonna push low's top, so high is gonna push low's top, low's top is three, so we're gonna, high is gonna add three also. Okay, now we're gonna pop low, so this is gonna be gone. Three is gonna be gone and we're gonna have that. Is the max heap, the size of the max heap smaller than the size of the min heap? Yup, see number of elements in max heap is one, number of elements in min heap is two, so we need to remove the top node and put it, replace it here, so we're gonna move the top node two and add it to here, okay? So now we have one, two, and then three here, okay? So now let's get rid of this, so we have one, two, and then we have three here, okay? So now, now we go to four. When you do four, we push the low push four. No, this is a max heap. My bad, my bad, my bad, and three here. Low push four, so then four gets pushed, so we're going to have four here, and one, two, and there's gonna be three here, okay? High is going to, yeah, okay. High push, low's top, low's top's four, so the name is gonna have four here. Low pop, so we're gonna pop four out, so this goes back to become two, one, okay? It's gonna reorganize become one, two, one. Is a number of values here smaller than the number of values here, nope, they're not, so we don't do anything. And let's go to five, five, low push five, so low is gonna have five, so we need to reorganize this. It's gonna be five, one, two, five, one, two, okay? High's gonna push the top, which will be five, so then we're gonna have five added to the right side here. Low dot pop, so then we're gonna remove five, so then it's gonna rebalance itself and become two, one. Rebalance will become two, one, okay? And then is a number of elements in low smaller than the number of elements in high? Yup, we have two elements in low, and we're three elements in high, so what we're gonna do is we're gonna, we're going to push the top value, remove the three, and put it into the max heap, so move the three, and then we're gonna put three into this one, so then in the end we have, here's what we're gonna have in the end, three, one, two, and then four, five on the left side, so in the end we're gonna have three, so the min, three, one, two, and then we're gonna have four, four, and then five, okay? And yeah, that's all we have to do, okay? So then the left side will have three elements, the right side will have two elements, and yeah, that's it. Now if we want the maximum size, now if we want the maximum number of elements, right? Now that if we want the median, right, we just have to check is the number of elements on the left side greater than the number of elements on the right side, so if they are, that means that the size of this array is odd, right? The size of the array is odd, so then we just have to take the root, so we have to take three and return three, okay? And if they're not, that means that both of the sides are even, right? They both have the same number of elements, so in that case we just add them up and divide it by two. We just take the root of both of these, add them up and divide it by two, okay? So yeah, that's how this two heaps pattern works. I hope you guys understand this video. It is, this pattern is not that difficult to understand. Make sure you have two heaps, and you could use this to find medians, you could use this to find anything else, and I hope you guys enjoy this video. Yeah, I'll just show you guys a code again. This is the code, the low is the max heap, high is the min heap, and that's the priority queue there, and then yeah, this is how the code works. This whole low.push num, then high.push low's top, which is just like getting the values at the top, and then removing the values on the, removing the root on the max heap, and just balancing it, and then we have to make sure that the low has one more value than the high, if it needs it, like if it needs to have one more value than the high, right, then we do that, all right? Now if you wanna find the median, we check is the max heaps size greater than the min heaps size, and if it is, we just return the root of the max heap, otherwise, we just add them up and divide it by two. So in this case, we'll add them up and divide by two, and they multiply by 0.5, but you just add them up and divide by two, and they cast into a double because for them it was an integer before, but they cast into a double because it was an integer before. But yeah, that's what y'all have to do. I hope you guys enjoy this video. This is just how the two heaps coding interview pattern works. Ready to come subscribe? I'll show you guys later, peace.