 What's up guys Mike the coder here today we are going to go over another Google interview question remove all ones with row and column flips You're given a matrix of grid and in one operation You could choose any row or column and flip each value in that row or column So we could change all the zeros to ones or we could change all the ones to zeros Return true if it's possible to remove all ones from the grid using any number of operations or false otherwise Okay so How do you do this problem? so the thing about this problem is is that There's really nothing no way to do this problem unless Without just going through a bunch of examples and then seeing if you find the pattern and then once you have find the pattern It gets pretty obvious on how to do it So then yeah, you will just be able to figure it out So let's actually just go over the first example case. So Let's actually bring a paint I've noticed from looking at like the recording afterwards that my cursor doesn't actually get recorded for some reason and I'm trying to look into that, but Yeah, I don't know if I could fix that I'll see if I could look into that though But anyway, let's look at the first example. So we have this first example of The matrix of ones and zeros, right? So we have basically the question is saying We have this matrix of ones and zeros and in one operation We could either flip all the zeros to become ones or the ones to become zeros So we could choose like any row or column to do that So in this case what they did was they chose this row Okay, and they just made flipped all the zeros to become ones and ones to be zeros So here they made this to be zero This become one and this become zero Okay, then yeah, so that's what that's how they got this That's how they got this this part here Here, okay, so yeah, that's what they did and then afterwards they flipped this column this The second column right or the second column, but you're basically zero one first index column And they just made all these into zeros right so we could remember we could either flip all the zeros to become ones or We and then the ones become zeros right or we could yeah If we could flip all the zeros become ones or we could flip all the ones be zeros So yeah, so then after here this they flipped the second column to be all zeros and that's how they got this answer so that's the first test case and that's the reason why it returns true is because What they did was they flipped all the middle row and then they flipped the middle column so Flip all the zeros become ones and then yeah, yada yada and then here since they're all ones here They flipped it and it all become zeros Let's look at the second test case and see why it's impossible. Okay, so let's just look at the second test case So this is the second test case The second test case is this so let's say let's say if we try to just flip it so Let's just see what what if we just flip it. So let's take this and Here we're just gonna take this and we're just gonna flip it. So Let's say we just pick the first row and we flip it. So then this becomes zero Zero and then this becomes one. Okay Well as you notice once you flip the one like the first row and now they all become like zeros on It doesn't really make any sense to flip it again flip the first row again because then if you flip the first row again It'll just be go back to its original form, right? I'll just go back like if I flip if I flip the first row and I flipped it again. It'll go back to like being one one zero Right, it'll flip back to being one one zero and that's not what you want Like if I flip the first row again, and I flip it again This becomes zero this one zero and this becomes one again Yeah, the zero zero one would just become one one zero and that gives you the original Row again, so it makes no sense to flip the same row twice So that's what you notice first part. Okay, so if I flip one row It makes no sense to flip that row again That makes any sense, right? Cuz then you're just gonna get the same thing twice And another thing you realize is that we're trying to get all zeros, right? I believe So here what we're trying to do Return true if it's possible to remove all ones from grid without using any number operations So if we're trying to remove all ones from the grid and there's only ones and zeros That means we're trying to get to all zeros in the end, right? So we saw there was a one here so that it made sense to flip it, right? So then we come zero zero and then this this zero became one right because so ones become zeros and zeros and ones There we have this now if we try to flip The last column as you as you can see here now We have Bunches zeros and the only thing that is left that is not a zero is this one here, right? This is the one here that we flipped and the problem about that is that? If I try to flip This the one the one the column that has a one here It's literally just gonna give me the So here if I try to flip it again All right, so you have zero zero one. So this is zero zero one, right? After flipping now I want to flip it. So then I this becomes zero. This becomes one. This becomes one Now that I flipped it again. I Have more ones now. So if you realize that by looking at this, it's literally impossible to get all zeros because and we have We have a one in the we have like a one In the end here, right? We have a one in the end here So it really makes no sense. It's impossible to get all zeros because if we flip it this one again We're gonna have more ones, right and reality what we want is all zeros, okay? So yeah, so let's just go over the solution now and I could explain so going back to what I said earlier is that basically what happens is that if we choose a row and we flip it and Flipping it again won't really do anything because we'll go back to original So if we choose a row what we can do for the algorithm is that we could choose a row set a dependency and like saying okay, I'm just gonna flip it and If we have to flip it again It means that It's not possible, right? So like in this case if I had to flip this it becomes either one and then I Loop through it and try to you know, just go through every row and just flip those values If I have to flip this again But this first for same row again, then it's just not possible because It just means that there's just no way to do it, right? so what we could do is we could go through every row and You could either do this by row or column so I'm gonna do it by actually call them so what I'm gonna do is we're gonna go through each of these rows and We're just gonna flip the corresponding column that has a one in it. So if there's a one in this This way So if we look at the first row if there's a one we're just gonna flip the corresponding column So let's actually flip this column and then it will become I'll just quickly get rid of this. Yeah, okay. Whoops too far So Let's say we flip we look we go through this row and We see hey, is there a one here? There is a one. So what we're gonna do is we're just gonna flip the column So we come zero one One, okay, and then we go to the next one. We see a one. Yeah, we see a one. So we're just gonna flip So we see a one in this row, right? We see a one in this row. So what we're gonna do is we're gonna flip the column so I'm just gonna paste the exact same thing Here in the next iteration So now I'm gonna flip the this column flip with this column zero one one So this is gonna zero one one and then Yeah, and then the next one it's all zeros. So that's fine. So here we for the first row we literally just flipped All the zeros all the ones to become zeros. All right So now they are zeros and then now what we're gonna do is we're just gonna check if we had to flip the same thing again By going through the next row and seeing if we have to flip it again So if we did have to flip it again, we just have to then it's not possible So if we see another one here and we have to flip this column again It's not possible. So then we just return false. So there's a way to check that I'm checking that instead of looping it again. So if you look here Here's the solution of a code that I'll be honest I didn't I didn't actually thought of the solution myself, but this is a solution that someone else had to had Okay, so here We first loop through from zero to the number of rows and here what we're doing is that we're gonna actually just check the corresponding the zeroth row and Check its corresponding Each of its corresponding columns actually so here here zero one is the Zero one is the each of these Grid zero zero. It's just gonna check all the values looping from in this specific row In the zeroth row and then it's seeing a zero one there Then if there is and we'll flip the corresponding column So that's what this is doing then we'll flip that column and to flip it these loop through from j equals zero to the corresponding There's m rows and n columns, right? So we're just gonna loop through the Corresponding rows here and just say hey j of i is just gonna equal to not j of i So this is what this is gonna do is it's gonna flip the zero to become one and one to become zero So this is looping from zero to n. So that's That's this part That's these values. Okay That's these values. That's what the loop is doing. So it flips those that call call So now the rest of the rows should either be all zeros or all ones and if it's not then It's impossible to find a solution. Okay, so for here what they did checked is literally just check the sum for the other other rows is to see if it's all zeros or ones so If you look here in this after we flip them There's what he's gonna do is he's gonna just loop through from here to here and just check the sum of each of these rows So here the sum here is two and it's definitely not all Zeros are all ones right. It's definitely not zero and it's definitely not one. So this is definitely false Right. It's definitely not possible and then yeah, it would just return false. So to do that they left loop from one to the number of rows one of the number of rows and That's that's this part, right? it's going through these like number of rows here and Yeah, they're looping from one. So they're gonna loop only these these parts, right the bottom part And they're gonna loop from J is zero. So from here to the end and They're just gonna add up the sum and if it's not equal to zero and it's not equal to M, which is like the length of this length of the number of columns Then it's a false. Okay. So basically either has to be all zeros are all ones for that corresponding row Okay, so if it's all ones then the sum should equal to the length of the total number of rows a total number of columns, right? So if this was all ones, it would be one plus one plus one It would be three. So it would equal to the total number rows of three so Total number columns of three. So it would return It would return it would be M right? It would be M So since it's not equal to that and it's not equal to zero. It would return false. Okay So yeah, that's what this code would do and yeah in the end. It would just return true Return true. So yeah, this is not Pretty difficult to understand. It's not that difficult to understand to do this problem It really just requires just going through different test cases and just seeing if you could figure this problem out Yeah, and I hope you guys know this video very come subscribe, I'll check you guys later