 Today we're gonna do another lead code challenge. It's gonna be sort array by parity Give an array of a of non negative integers half of the integers in a are odd Half of the integers are even sort the array such that whenever a of i is odd i is odd whenever a of i is even i is even You can return to any answer the array that satisfy the condition Excuse me, so I'll give you a few guys seconds to do this So try to do this yourself, and then I'll explain my solution and how I did it Okay, so Right now. I'm gonna explain how I did this So first of all We're given array and then we need to for every odd integer okay for it for every If a of a of zero is odd if a of i is odd i is odd for every a of i is even i is even so in order to return this All we have to do is keep two indexes one is to keep track of the one that is odd The other one is to keep track of the one that is even Okay, so whenever you're going through the array if you find an odd one you just increment that Set the odd one to a of i if it's odd it set A of i if it's even set it a of i to even if it's even so I'm gonna create a new array. I'm gonna call this Vector int to return And I'm gonna go through a of i. Okay, so I'm gonna go through a of i for i equals zero i equal I left send eight outsize I plus plus And what am I gonna do? So I'm gonna go through Eight outsize I plus plus or check if a of i is even so I'm gonna do that. I'll do a of i mod to Is equal to zero Then that means it's even okay, so if it's even I'm gonna increment I'm gonna set by to return at I and my index of My odd index so so I'm gonna have a index for odd index and index for even so index odd That's gonna be one index even That's gonna be zero if it's even I'm gonna set to return at Index even is gonna equal to a of i Then I'm gonna do index even plus equal to two Else I'm gonna do to return at index odd is going to equal a of i And I'm gonna do index odd plus equal to After this ends I'm gonna return to return and this ideally this should work So I'm gonna do oh, yeah, I have to set the size to be eight outsize to be the same size Let's run the code and let's see what this does Ideally it should work Think it does. Okay. So yeah, the output is expected. Let's submit it hold up Hold up and it got accepted. So, yeah, that's how I did mine To Basically sort array by parody. I created my array with the same size I set my odd index and even index and every time it's an even I set my even index to equal Whatever value and I increment my even index by two If it's odd, then I set my to return index at odd Equal a of i and I increment index odd by two then I return to return So yeah, that's basically what I did Now if you want to see the actual solution I'll explain how to do that. So to do the actual solution They actually did not create a new array What they did was So what they do is essentially They're going to have an index for it j which is the odd index and then they if it's if the a of i is odd They're going to keep going while the Index of the odd index is odd and they're gonna plus equal to two And then after that they're gonna swap the two values between A of i and A of j so essentially For each even i They are trying to make A of i even to do it they would create An element from the odd slice and then they pass j through the odd slice until they find an even element and then they swap So essentially they're trying to swap the values of The odd values with the even values essentially Well, not odd values or even values. They're trying to whenever they see an an odd value. They're going to swap it with this, okay, so See you So first it is enough to put all elements even elements in the correct position Since all odd elements will be in the correct position place two So let's focus on A of zero a two a four We would like to have some partition where everything to the left is already correct and everything to the right is undecided So this idea works if we separate into two slices even is equal to a of zero a two a four An odd is equal to a one a three a five Our invariant would be everything that's less than i and the even slice is correct Everything that is less than j and the odd slice is correct So for each i they make a of i even to do this They would create an element from the odd slice pass j through the odd slice until they find an even element then they swap The invariant is maintained so the algorithm is correct. Okay, so Okay, so here If whatever value at i is odd they're going to have a j And they're for every if the value at j is odd Then add by two to go to the next next two elements, okay then They keep doing that until A at j is not odd. So after this afj is not going to be odd It's going to be even So this is this is going to find afj uh Is going to find the first j value that's going to be even that Has an odd index. Okay, so this is going to be the first value of j that is even afj that is even But it's going to have an odd index Once they have that they're going to swap afi which afi is an odd value Okay, so afi is afi is an odd value that has an even index. They're going to swap that with An even index. Okay, so an even value That has an odd index. Oh, okay, so okay Let me explain this So they're going the first so Essentially they're They're creating an index i That starts from zero and i this is going to go every by two. So this is going to go through every even index They're going to check Whenever they hit a value that is odd That has an even index What they're going to do Is there they are going to? check if There's a value that is Odd that has an odd index right and they're going to keep going Keep going plus equal to two until they reach An even value that has an odd index Okay So then after that they would swap these together Okay, so this would I don't know if i'm explaining this correctly, but this essentially they are trying to find the an odd value in With an even index and swap it with an even value that has an odd index That's what they're doing then they're returning the array. Okay So yeah, that's what this solution is doing Okay, wow that sounds really complicated, but it's actually not that complicated. Okay, so I'll do the exact same thing As a solution This is going to be our odd index Going through every odd index And this is going to be our even index Going to go through every even index If we see a value that's odd Going through every even index What we're going to do Is we are going to find the value That's odd that has an odd index Uh, no find the value that's Whoops my bad When I keep going until we find a value that's even that has an odd index Yeah, so We're going to keep going so while this is odd it's going to keep plus equal to two Keep going for every odd index of j. So it's going to be one three five seven nine They're going to keep doing that until we find an even value of a of j. That's has an odd index Once we find those two values, we're going to swap these two values. So we're going to create a temp variable ai A of i is going to equal to a of j A of j is going to equal to temp After that after they swap those they're going to return a Okay, sounds pretty basic pretty easy. Let me just Take this out Take this out Take this out Take this out Let's submit compile error. Okay control reaches a non void function. Oh wait. Okay, so I'm supposed to return a afterwards I have an extra parentheses Okay, okay Right here submit the code Got accepted And yeah, there you have it. That's all you have to do For this problem ready cop subscribe. I'll show you