 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna do another league code challenge This is called peak index in a mountain array. Let's call an array a a mountain if the following properties holds There's gonna the the length has to be greater than equal to three and there exists a index I Such that all the values on the left side are less than it and all the values on the right side Are also less than it so essentially all the values on the right side are gonna be sorted decreasing order but it's less than whatever value That we're trying to find and all the values on left side are sorted in increasing order But it is less than of what our current value is So given an array that is definitely a mountain return an index I if any There's such that this exists Okay, so in this case all the values on the right side is less than the current value of one All the values left side is less than current value of one. So they return one Here we have all the values on the left side here, okay is less than all the values of the right side of the Current index that we're at. Okay, so we're returning the index, right? So here zero one. This is that's two so between two all the values on the left side is gonna be less than two all the values on the right side is Also less than two but this is decreasing by this is decreasing or descending order So this is decreasing. We got one zero All right guys, so I am going to see Give you time to see if you could figure this out and if you can Come back an hour later. If you can't and I'll explain how what I would do in this situation So what you could do is basically just create an index called I Equal zero and then just loop through it through it while I is less than eight odd size We'll go through the array and every time Such that Every time that a at i is less than a at i plus one Whoops, then we're going to increment i So we're this is essentially saying that oh, I should go to i minus the length minus one So yeah, this is essentially saying that we're gonna start from zero and we're gonna keep going to the right and every time that we see a Value that is less the current value left side is right is less than the right side We're gonna keep going then in the once we get to one where it's not because the right side The right side the our current value is going to be greater than some value Of the index right next to it sooner or later Then this this would stop. Okay? so then After that What it's gonna do is it keep going until we go to that and then I'll just return i So let's see if this works you could do it this way. I hope this works If it doesn't work then let's see Daily they should work. Oh time when I exceed it was that Okay, okay, I'll hold up. Okay, so let's see. Let's go back to it i is zero i is less than it if i Is less than i a li plus one should increase i We should keep going until it reaches to the Time limit see there's a let's see zero zero zero. Let's see your one i is less than eight outsize minus one Okay, so essentially I'm gonna keep going right. This shouldn't be time when exceeded though Let's see zero. Let's see. Let's see in the test case zero zero is less than the size three zero less than one yeah plus one Okay, oh, whoops, okay, this should be in here whoops my bad All right, then i plus plus Now let's run it Okay. Yeah, so that works Submit it see if it works And it got accepted essentially this is saying the reason why my eye This part has to be over here is because otherwise the loop would essentially get stuck there And I would never get incremented so that's why I had to put that there but essentially you're saying is that while My beginning index I start from the beginning and I go to the end while My current value is less than the The value on the right is the the one after it then I'm gonna keep increasing my index then in the end I will return my index that's essentially what I'm saying in this case because sooner or later you'll get to a part where Our current value is going to be greater than this and that's the one we have to return So this is my implementation of this. Let's see what the solution is So yeah, so basically my implementation was like this Okay, so what the better approach is is actually to use binary search and Remember how binary search works essentially is is that if I have an beginning index right and then Yeah, let's see and Low is equal to zero High is gonna equal to eight outsize Minus one Let me just type this out for you guys so how binary search is if you remember we take the middle index and Essentially as we Essentially what we do is we add Hold up No is equal to mid plus one High is equal to mid Then we return low so Let me first run this code and then I'll explain to you guys what it's doing and why binary search works in this case So remember how binary search works is that if you're given whatever number You want to search for it, right and essentially is is that it lets you binary search lets you discard all the values Between the middle so it you search through the middle one the current middle index and then if it's less than it Then we know all the values on the left side Can't be it right so then then you go to the the right the right side the middle of the right side and then you search of that and then Then if that doesn't work We go through all the values on that the left side and then it would keep going and keep going right So that that was how binary search works reason why it works here because let's I'll show you guys We start our low here, and this is our high This is our high right Now is the middle Which is high minus low divided by two so this is zero one two three This is the index two minus zero Is two divided by two is one one plus zero is one so this is the middle This is the middle middle Okay, is the middle Less than to the right one. It's not So then I set my high to be the middle so now our high is going to be in this this middle Right so now We that means that we could discard whatever we're searching on the right side For a of i Minus a of i less than a of i plus one high at the high is now equal to middle Now is low less than high. Yes, it is. What is our middle? Middle is going to be high minus low which is one minus zero Which is one divided by two Which is zero zero plus zero is zero is a at zero less than a at zero plus one Which is one. Yes, this zero is less than one Okay, so then our low is now going to be middle plus one So our middle was here No, our middle was zero here right now Because of what we were doing our middle was here right now our our a at middle plus one Our low is going to equal a at middle plus one so low is going to be here and is low less than high Yes, it is No, no, it isn't and then it returns low so it returns this so why does it was work because think about this Every time we go through binary search All the values it's going to be true true true true true and then it's going to be false false false false Because at this point between here and here we know these values are not less than A of i is not less than a of i plus one like in this scenario here in this scenario here a One is not less than zero So we know these whatever values on this right side is going to return false Right if we were to search through the right side So because we know that those the values on the right side will return false Once we get to that condition we could just set our high and equal to middle middle and then we could you stop our condition once we return a a True a true true true and then once we return the index of false in this case false Then we could just stop our condition and then set that equal To our low or we could change our low and return that part of our index Essentially, that's what we're doing. So I could Here I'll show it to you guys one more time. I'll put it in visual studio and I'll explain it again The reason why we're able to do this is because is that on the right side All these values are going to return false right, so if in our comparison Once we get to a value that is returning false we could change our search to not do that right, so we're going to always maintain this value of Searching here Our binary search will keep going whenever this this scenario is true Once we get to a part where these two intersect where high is now like at the point of low is not when high is not greater than low anymore Where it's about Either equal or less than that would mean our condition would now return True return false and then because one which we get our first false That means that we could use binary search and then return our low Which is our the index where you're currently at where the first time it's false and Once you do that you're able to determine The point where it stops increasing at its peak the first false that occurs So yeah, that's basically how you do it. I don't know if I should really Show it you guys on visual studio because that's that's basically how you would do it Yeah If you have any trouble if you guys want if any of you guys know how to explain this better than I do Please put it in a comment section below why binary search works this in this case But I think I explained it it's mainly because you want to find the first false in this scenario Where this returns false and we're do that you could change your low and high index Such that our high can now point to the middle. So we're gonna discard all the values where it is not Where it's true essentially we could discard those values, okay? So yeah, where you come subscribe. I'll check you guys later. Peace