 I'm gonna go over the next problem of division two of 637 to be the problem statement is really hard to understand But I'll try to explain as much as I can pretty much All they want us to do is to find the maximum number of peaks for a sub array size k given the Length of the array is length n. But what do I mean by that? So a peak? Just means that whatever value before and after it is less than it So number four is a peak because two and one are both less than the current value four And this four is also a peak because two and one the values before and after it are both less than four Okay, we are not considering values way way after it. Okay. It has to just be One less than the current index and one greater than the current. So if I have like an eight over here Let's add an eight over here. We're not considering that checking whether this is a peak or not Okay, we are only checking whether the left side One left then the current index is less than the current element and the right side The one greater than the current index is great is less than the current element also So that's what a peak is now. We want to find the maximum Number of peaks with a sub array size k Okay, so what do I mean by that a sub array continue Sub array what I mean by a sub array. I mean contiguous. Okay, so I mean from Here, let's say this One two three four five six This is a contiguous sub array of size k equal to six Okay, and the reason why is because There are six elements of size k in this sub array one two three four five six That's what I mean by that. We are not picking random elements out of this array We are only checking for the contiguous sub array Of size k and in this case, it is a sub array of size k because it has six elements one two three four five six and they're Immediately right after each other, right? Okay, we're not picking random elements. That's what I mean So how you would how would you do this problem? Well, one thing you could do is you could go through every single sub array of size k Find the number of peaks in each sub array Then compare them together and then find the maximum peak Okay, so in this case, I would just go through from here to here Which is a size six which is one two three four five six find the number of peaks here in this case, it would be one Because there's one four There's only one peak where the both left side and the right side are both less than the current element And yeah We are also not including peaks on the bounds The bounds okay, so if if this was a peak one Let's say this was a peak We're not including the peaks on the left side and the right side. Okay, so this peak This right side is a peak, right? There's two one This four is a peak Right, but we're not including that. We're not including on the peaks on bounds Okay, and That's the reason why we were not including is because the problem statement says that okay We are we are only considering peaks between these these both This the left side and the right side. Okay include in inside these bounds. We're not including peaks Uh At the current bounds. Okay, so that's what I mean by that so Let's keep going So this was only had one peak for the first contiguous subway. We're going to start from here And then go to the right and then find the number of peaks so in this case, there's two because If I were to only count the Number of peaks between these two bounds not including them Uh, there's only two right. There's only one for it, which two and one are both Less than four and this four two and one are both less than four So the next one will be two All right, and then I would have to count from here to here And then find the number of Peaks between here not including the two bounds in this case four is a peak because Two is uh less than two and two and one are both less than four right two and one less than four All right, and so we only have one peak All right, and then our maximum number of peaks would be two Okay, given size k equal six. Okay Now how would you brute force this using code? Because I showed you how to do it with pen and paper now. Let's I'll show you guys how to do it with code Well, let's count the number of sub arrays we had to look for So in this case, we only had to look for three sub arrays reason why because if I were to start from here Again if I keep going Am I like keep iterating here? I can't go through six contiguous more from here Right like after four if I were to try to find another sub array of size six One two three four five the six. There's no more because it's out of bounds. All right, so there's no there's no sub array after the Of the index of this index. Okay after four. There's no sub array after that So what are the number of sub arrays? Well, we the number of sub arrays we checked was only three. Okay, so the number of sub arrays Is three and how did we get three? Well, uh, if we look back at our values of variables of n and k Right, uh eight minus six is two two plus one is three So number of sub arrays is going to be eight minus six Plus one two plus one is equal to three. Okay, so the The number of sub arrays given size variables n and k if we plug it in would be n minus k plus one Okay, so that's the number of sub arrays if you were to brute force it Okay, so you would loop from one to n minus k plus one Right, and then you would have to check every single bounds All right, so how do we check every bounds now? Because we have to we only could check the bounds from left side and the right side so we have to consider the bounds of Left and right so, uh, let's look at our Our array again. Okay We're i'm going to index from values Of one the reason why i'm doing this is one two three four five six seven eight The reason why i'm indexing from one is because it's easier to handle I don't have to check like i don't know zero I think zero indexing for zero is kind of hard to handle, but i'm going to index from one So our left left side we started from that we had to check is one index of one Because we're starting a left side that we had to check for index one What is our last index that we had to check? It was six Right if you from one to six, so that would be one two three four five six. Yeah, so it's six and what was our Next value that we had to check two Two was the index that we started the next index we had to start from so let's do and then what did we end at? We ended at seven And then three is our next value that we had to check And then what did we end at? We ended at eight And then four you can't end it. Okay, so let's think of a Given the starting value of Left side of the starting value it went one two three, which is Exactly what the number of subways we had to check Um now we had to find the right side the right side of the ending point the right bound that we had to end at so We don't actually add starting plus k Or six Because look we check from one to six, right? So we didn't check from one to seven We didn't do one plus six And went to seven for our Our right side what we did was we did the left side or start one plus five So our right side is actually going to be equal to One plus five Which is six right our right side is one plus five which is six and how do we get five? Five is just k minus one. Okay, so our k is six We add k minus one. So if our left side if left bound is start Uh, let's not you start if our left bound is left Right our right bound that we're checking is going to equal to left Plus k minus one. Okay. So this is our right side. So if I were to just loop from Uh, left equal to one up to n minus k plus one, right? And then I have another for loop from left looping up to left, uh Left up to left plus k minus one That would get me all the Values to sum up for every single peak for each sub array and then I could just return that But that that problem is that it's o of n squared and that's not what we really want to do So a better way is actually to use something called prefix sum So what I'm going to do is I'm going to count the number of peaks every time we have The array so here I'm going to use rewrite the array again Okay, so I'm going to keep track of each peak So to do that, I'm going to just loop through through the array and then if there's a peak I'll set it as one if there's no peak. I'll put a zero. So This is not a peak. Okay. There's no value before it. Uh, two is not a peak because four is greater than two Four is a great peak because two and one are both great less than four. So that's one The one's not a peak Two is not a peak because one of four are not less than two Four is a peak because two and one are less than four. Okay One is not a peak because four and two are not less than one. So zero Two is two is on the right side and it's a little n so zero. Okay So now I have the each cap track of each peak now all I have to do is just Do a prefix sum so I'm going to add up every single sum So I'm going to sum of the current maximum number of peaks at each index So zero plus zero zero zero plus zero zero zero plus one is one one plus zero is one One plus one is two two plus zero is two Two plus zero is two. Okay. So now I have a prefix sum Okay, so now now that I have a prefix sum Now all I have to do is just find the number of peaks and subtract at whichever My boundaries are and then I would get the right answer. Okay, so What is my how do I find the the boundaries that I'm subtracting for each prefix sum? Okay, so Let's look at index uh one two three four five six seven eight Okay, let's look at this case again that we had to find the maximum number of Peaks from one to six. Okay In this case of one to six, I need to find the maximum number of peaks Okay, but I don't want to include the bounds that I'm at the left and right bounds So if I'm going to find the maximum number of peaks And I already have my prefix sum that told tells me each maximum peak at a certain index If I want to find the maximum peaks of one to six I let's look at this I can't just use six. All right, my prefix. I'm at six. The reason why I can't use prefix sum at six is because prefix sum at six Includes the boundary of six. Okay. It includes the boundary of my last bound of Of my last peak at the current six. So I can't do that. So what I have to do I would have to use my prefix sum One less than six. So I have to use five Okay, so my prefix sum of that is five. So I have to do prefix sum of five Now I don't want to include my left bound also Right because the problems they've been told us we can't include prefix some Can include peaks at the left side also And um, so how would I do that? Well All I have to do is just subtract the prefix sum at my left one side My left bound one and that makes sense. All right, uh, so I'll show you guys why this makes sense So we want to find the number the maximum number of peaks between one and six That's this is the maximum of peaks we we need to find from one to six, right one to six But we don't want to include the one At each bounds of one and six Okay, so what do I do? So I find the maximum Prefix sum at five and so if I use Maximum prefix sum of five. I'm not gonna include the six Okay, so that that pushes my prefix sum to have this one to five But I don't want to include the prefix sum of one also So I'm going to subtract all the sums that came up to one So if I were to subtract all the sums that came up to one so I get rid of this these two The my prefix sum My number of peaks Of my prefix sum Left over is going to be between two and five and that's what we want to include Okay, we want to include the maximum number of peaks between two and five We don't want to include the boundaries of one at six. So that's why it's this prefix sum of five Which is this one and then subtract by prefix sum of one. Okay, so that's what this gets here now uh If I were to Name these with variable names, right because it's not just one six five and one, right, uh, it depends on what we're looping up to Let's call this left and let's call this right My prefix sum I'm going to subtract of it's going to be Right minus one Right because it's the index of prefix sum of right minus one right minus one five minus one, okay Then it's going to be subtract by prefix sum of the left Okay, so right minus one and the left now going back to what we said before Of the equations that we wrote for left and right Our right value is actually going to be left plus k minus one That we found out so I'm going to substitute that So my right Is going to actually be left plus k Minus one and then I'm subtracting one again because I'm still using substitution Right, I look our right was left minus k left plus k minus one and then I have to subtract one again because That's so using substitution that we substitute now. I guess I'm subtract prefix sum of left Okay, and that's basically our answer. It'll just be prefix sum of left Plus k minus two minus prefix sum of left And that'll be our answer Okay, that'll just be our answer and then our left is going to loop from left is going to loop from One to n minus k plus one Okay, and yeah, it's just be o of n solution because after We will only take o of n to create our prefix sum and it'll be o of n to Loop through from one to n minus k plus one and then find each prefix sum of each of these and then Find the maximum one calculating each of the max That's how you do this problem. I hope you guys understand. I'll show you guys the code now Okay guys, so I'm going to go over the code with you guys right now. So This I created a vector int which pretty much is just the Size of the data array that we're given And I'm going to index it from index one So that's why my size is going to be n plus one because that's that's how you index it from one And then I'm going to read in every single Loop from one including n up to an including n Read in every single element and that'll be index from one to that Then I'm going to loop from two to n minus one and the reason why I'm going to do from two to n minus one is because This is going to check every single peak. Okay from the beginning to the end Uh, I do n minus one because I don't want to get out of bounds when I do i plus one. Okay, so Here I do it if statement if the little index on the left side is Less than the current value at i and the index at the right side i plus one is also less than the current value At i okay, then I do peaks at i set it to one. Okay in c in c plus plus the array is automatically uh Becomes like zero. So When I set peaks at one, I'll just have the values at our peaks Going to have value equal to one. Okay Then I create my prefix sum and to do that I just do prefix at i loop from one including the n the length of the array and then I'm going to set the i minus one plus the current value of peaks after that I have a maximum values For the value for finding every single max peak and then I have a left bound because they also wanted you to print out the left Bound I don't know why but they want you to do that. Okay So then I loop from i equal to one Up to less than or equal to n minus k plus one which I showed you guys already why I did that Using pen and paper and then I do I find my current max which is the current maximum peaks I'll take i plus k minus two minus prefix sum of i and that'll get me my current max Then I check if it's greater than my current max I'm going to set my left bound to equal my current index that i'm at And then I set my max values to equal to that new value new current max. Okay, so that'll get me my max max max number peaks, okay After this loop is over. I just print out max values plus one and then left bound The reason why I do print out max values plus one because in the problem statement, they want us to print out the plus one They want us to print out the number of peaks plus one I don't know why but that's what they wanted us to do And then I print out the left bound the left bound is just whatever index that you last left on But yeah, that's it. I hope you guys understand this code Uh ray com subscribe. I'll see you guys later. Peace