 What's up guys, my name is Michael Lane and welcome to my YouTube channel today. We're gonna go over move zeros So pretty much I haven't really I haven't really been keeping up with the leak code 30-day challenge I was really just honestly just like just Scrolling along and just like not really caring about it So I'm gonna try to like keep up with it now So I have to make another video about the 30-day challenge because like I was too to two vids behind two problems behind So yeah Here's another video move zeros So you're given an array nums and you have to write a function to move all zero to the end while maintaining the relative order of the non Zero elements, so we have zero one zero three twelve and then the one three and twelve got moved to the beginning and the rest are Zeros so I already solved this problem, but I'm gonna explain my brute force solution and pretty much explain how to how I Came up with it. So I first notice is that the all the functions All the values that are not zero are moved into the beginning and the ones are That are zero are all moved to the end and we have to maintain the same order So what I'm gonna do is I'm gonna loop from I'm gonna loop from the beginning to the end Whoops Loop from the beginning to the end then I'm gonna find my first zero. So if I find my first zero Find a zero So I'm gonna first loop through the beginning to the end if I find my first zero I'm going to from their loop from that point to the end and find the first one Yeah first non-zero Okay, so I'm gonna so once I find a zero I'm gonna loop from then on to later on and find the first non-zero and then swap it So what's that gonna do? Is that gonna put that's gonna put all the? All the non-zeroes in the beginning and the zeroes in the end, okay? So because I got at the end of the loop All the zeros are gonna be in the end So first I'm gonna find my first zero then I'm gonna loop from that position Onward and find the next non-zero and then I'm gonna swap them together So from here, I'm gonna find from this position onward is gonna be I plus one J less than thumbs dot size J plus plus. I'm going to so from this position onward I'm gonna check if my position I'm currently at Has no zero then I'm gonna swap it Swap the two values. Okay, and then after that I'm just gonna break because then I'm done. I already found it and then it goes on to the next zero All right, so to do this I need to create a function called swap So I'm gonna do do this void swap and it's gonna pass in two two values And it's I'm just gonna swap those two values. All right, so here Yeah, I'm also gonna pass in the vector. So then it actually changes the array. So Yeah, I'm gonna create us This variable called temp is gonna equal to my first value and then my first value I'm going to set it equal to Next value and then I'm going to set the next value is equal to temp Yeah, so that swaps the two this is this code is gonna swap the two values from the first index to the second one Right, so it's gonna swap these two values. Okay So yeah, then after that it should be that's it. That's all you have to do. Let's submit the code Compiler, what's a compiler swap? Oh, yeah, so I need to pass in a vector my nums my nums vector taking a while And I got accepted so let's look at the details There's might there's actually a better way to do it, but this is pretty much most straightforward ways to brute force it Oh, well, let's see what it was my runtime 72 milliseconds memory uses I Don't get it. I Don't know what this means. Okay So worst-case scenario, let's explain the worst-case scenario Worst-case scenario is all the zeros are going to be in the beginning and then it's all the n squared because I have to go through The first for loop and then swap it Yeah, it'll literally go to the first for loop then it goes to the second for loop and after swap all the values over and over again So worst-case scenario is when all the All the values are all the zeros are in the beginning and all the non-zeros are in the end and it'll be n squared best-case scenario is that that all the values are That I get into this for loop I find a zero right again this for loop and I just swap it once so the the The non-zero is right beside the first zero right that's the best-case scenario And then it'll just literally take O of n because then I go into this for loop and I just find non-zero Which is right next to it and I swap it so that's the best-case scenario worst-case scenario is O of n squared So now let's think about a better optimal solution Which is to do this with one for loop and I'm gonna erase this and then I'm going to show you guys how to do that So we first know that based on our first Vector right our array that all the zero So we want to move all the Zeros to the end while maintaining the order of non-zero elements So what we could do is we couldn't maintain an index starting from the beginning and that that beginning index is just going to store All the non-zero non-zero values in the beginning and then I'm gonna update the index So then I'm gonna put like one to At index zero and I put three and x2 and I put 12 at index To okay, so that will put all the Non-zeros in the beginning then we could just set all the ones at the end from that index onward to the to be zero So I'm gonna do right that code here also So I'm gonna first start with the beginning index I'll just have that be beginning. So first I'm going to do is if nums at I is not zero Then I'm just going to set nums at begin is going to equal to Nums at I so that's going to set the whatever Index at the beginning to be that the non-zero value right because I'm gonna find non-zero value And I'm gonna set it into as a beginning then after that I'm gonna do beginning plus plus and What that does is that it's gonna update the beginning index so then the next non-zero value I'm gonna place it at index one right so this next index value next value That's non-zero three is going to be put at index one and then 12 is going to be put at index two. So, yeah Whoops So that's what this code is going to do then after this point on I'm going to a loop from Intj is going to equal to oh, we're not I shouldn't do this while begin While begin is now less than nums dot size I'm going to set all the values at begin is now going to equal to zero So nums at begin is now going to equal zero And then begin plus plus So what this does is it's gonna set the rest of the values to be zero After I put all the non-zeros in the beginning. All right, so after I put all the non-zeros in the beginning I'm gonna from my index that I stopped at so after one three and twelve gets put into the beginning So then I have zero one and two I'm going to start from two zero one and two right. I'm gonna start from two two plus one actually so Yeah, so okay, so after all of my non-zeros put in the beginning. I have the index zero one two That's where I stopped at I stopped at index two and I'm gonna set the rest of them from here on forward to be zero So I actually should do a begin plus plus Here just to make sure that It's like the one after it, but yeah, that's what this code will do. Let's submit. Let's see if this works. I think it does Not sure though not a hundred percent sure Wrong answer. What did we do one three two? Oh wait? Oh, okay, then wait. Oh, I didn't set this one to be Oh, okay, and we don't you have to do begin plus plus Hold up Yeah, and I got accepted Okay, so we don't actually have to do begin plus plus over here reason why I thought I had to update the begin index again But we don't have to from here on forward. I'm gonna set everything else afterwards to be zero From after begin to the after forward so from the at this point after all the non-zeros get put in the beginning my index is gonna be This point at this point zero one two three at this point, right? And then I'm going to set them from here on forward to the end. I set them all to zero and yeah This is going to have Let's see worst case scenario is is that all the well, okay, yeah worst case scenario is that pretty much you have All them are zeros in the beginning and then I have to set Yeah, I just we know all the non-zeros are gonna be at the end And then I set the ones in the beginning and then this for loop goes Goes completely forward and here then it's another oven score over and solution So it's O of n plus O of n and that's O of n. So yeah, I think this is the best optimal solution O of n position without Yeah, well maintain the order Okay. Yeah. Yeah, that's pretty much how you do this problem you put all the put all the non-zeros in the beginning and then from then on that point forward You maintain the index where all the non-zeros in the beginning had stopped and then from that point forward You're gonna set them all to have value zero. So that's what this loop does, okay? So that's how you do this problem ray com subscribe. I'll check you guys later. Peace