 What's up guys my is Michael and welcome to my YouTube channel today. We're gonna do another lead code grind challenge This is flipping an image and this is actually really easy when you when you read it It's kind of really easy But if you read the the input and output and exclamation sounds really difficult It's actually really easy given in our binary matrix a we want to flip the matrix horizontally Then invert it and return the resulting image. So To flip an image horizontally it means that the row of the image is reversed. So Flipping 1 1 0 results in 0 1 1. Okay, so basically this is what they're saying is that This Like whatever's last becomes first in the middle becomes a second and blah blah blah. That's what they're saying So there's so they were basically flipping everything from like 1 1 0 becomes 0 1 1 and yeah Whatever is the beginning become last last becomes then yeah, so that's basically what it is to invert an image means that Each zero is replaced by one and each one is replaced by zero. So for example inverting 0 1 1 results in 1 0 0 So this is basically the whole algorithm You you want to flip the image horizontally and Then invert it by replacing all the zeros with ones and ones with the zeros and then return it So I'm gonna give you guys a few minutes to do this and then I'll explain the solution. Okay, so yeah do this. Okay, so I hope you guys Tried attempt to do this at least normally I give myself at least one hour or two hours to do it If I can't figure it out, I look at up the solution. All right, so we're giving me a matrix And we want to flip it horizontally then invert it and then return it Okay, so I'm gonna return and create a matrix called To return this is basically the right matrix. I'm a return. Okay, so basically I want to flip I Want to flip each row of the image is reversed and then I need to replace every zero with one and one with zero Okay, so to do that I have to go through every row in the matrix. So I'm gonna do int row equals zero row is less than a dot size row plus plus This is gonna give me each row in the matrix So what am I gonna do I'm gonna do in a current row is gonna equal to a at row Okay, so this is gonna be the current row. What am I gonna do? I'm gonna go through each Actually, let me see do do do do do do do Flip an image horizontally means each row of the image is reversed and then okay. Okay, so this is easy Yeah All I have to do is reverse each each Reverse each image each row and then I have to replace each of zero every zero by one and one by zero So in order to that There's a function called reverse actually in C plus plus reverse an array C plus plus and And There's a way to do it STL Yeah, reverse start index last index. So Yeah, you just reverse the array you just call reverse on Current row pass and begin that's the that's the Iterator from the beginning current and then the begin and that's the iterator of the end and this ideally should reverse Whatever row we're at so that the first space last last was first yada yada yada, then I'm gonna go through my current row my each row my every value in the row and Basically, I'm gonna check if it's a zero then I change it to a one if it's equal to zero I make it to a one and Else if if it's a one I make it into zero Yeah After I After I do that invert an image. I'm gonna add it to My matrix that I'm returning so to return that Push back. I think you could do this. Yeah, push back Current row and then that should be it That's basically this whole algorithm I'm gonna see if I could optimize this afterwards see if I don't have to create a new array and return it you might be able to just do this by Not having to just pass Instead of creating a new array you could just change a but anyway, let's just see if this works Yep, it works. Okay, let's submit it and it got accepted Yeah, that's basically it Let's see if we could not use to return and see what happens Let's see if instead of creating a new array to new matrix. Let's see if we could just Change a so Let's say a query current row points to a arrow. Let's reverse a arrow Go through every current row Let's what if I do What if I just return a? Let's see if I could just return a See if this works. I'm not sure it might it might not I'm not sure actually mainly cuz this changes it to a reference and if it's a reference No, it doesn't okay. So yeah Let me see what if I don't do this, okay, my computer is lagging really badly This is good. This is a good thought exercise. What if I just do this? Yeah, and then I do a at row at I oh, okay. This is gonna be really really really Really Messy code, but this is really messy code, but Let's see if it works. Okay Basically, I'm just not gonna use current row and I'm basically just gonna try to change my original My original array Instead of returning a new one if it doesn't work, then we'll just go back to our previous solution. Yeah, whoops Man is lagging really badly right now. Oh, I think it works Okay. Yeah, it works. Oh wow. I didn't expect that to work, but okay. Yeah Basically what I did was now instead of creating a new array and returning it I Just in adding whatever I changed. I just reversed My I reversed whatever row as I was on right original. I just changed the original array So I reversed whatever row is on then I went through every single row Every value in each row. This is what this this forlip is doing and then I change if it's a zero Like whatever value at each row that make it one if it's one I make a zero and then I return the array Yeah, that got accepted. That was pretty basic Rate com subscribe. I hope you guys enjoy this video. It's really short. But yeah, that's how you do flipping an image Let me know what what solution you guys have you might have one where it's faster than a hundred of the C++ Submissions, but I don't know. I don't know how else you would do this without what I would just did And I think you create your own reverse method and see if that is faster than there were this reverse method But I don't know if that's worth it or not. Yeah Rate com subscribe and check you guys later. All right guys So originally I was gonna end the video on that but there's an even better solution than what I just optimized so first of all, we don't really need this because Well since we already know in the problem if it's zero then we change it one if it's one we make it a zero So we don't need that check so that prevents a little more a little more efficient I guess but it's not that much, but there's an even better solution There's a way to do this where you would only go to the halfway point then you would copy whatever value From your what you're currently at to this halfway point so This is that this is the solution that they gave us and I'm going to basically implement it in C++ with you guys so What are we gonna do first? Well, this is gonna get the length of the number of columns in the array which in the matrix which is I could do that I guess but that really is not necessary. That's the same thing as a at row dot size, but Why not? Okay number of columns Okay, this is a at zero dot size. Okay, so they're assuming that whatever whatever Whatever number of columns that would just get rid of this. Okay, but that doesn't that doesn't matter What are we gonna do? So we get the number of rows and then we're going to go through every row in the array in the matrix Which is basically what he does and then we're gonna instead of going to Instead of reversing it and then flipping the was zero so ones and ones into zeros. We're not gonna do this What we're gonna do is we're going to go to the halfway point. So we're gonna go from For every column Columns less than number of columns we are going to plus one and Divide it by two And what does this do? This is basically going to This is this means that we're gonna go to the halfway point halfway of whatever. I'm at and We are going to swap the values from this is basically reversing the array, but because we're only going to the halfway point It's much more efficient than reversing there for the array and then then changing the zeros and ones and ones is yours. Okay, so This is essentially reversing the array because if you were to think of this mathematically I don't know who who thought of the solution, but this is actually a really genius idea basically if I were to have like a list of numbers one two three four If I start here and I go to the This is my index. Let's say zero is my eye Right, and I go one I Go up to halfway point. So the size is four. So I only go up to here Right, I only go up to here If I go up to halfway point and I just subtract the and I just copy this value to here and Copy this value to here. That's basically reversing the array Do you guys see see what I'm saying and how you would do that usually would you would get your whatever index you're currently at? And you're going to set it equal to the length for You go up to the halfway point, right? you then you set your You the length Whatever your current index at you swap it with the value at length, which is In this case for minus the current index minus one, right? So if this is at zero the end would be four I Want to swap So if I start at zero here, the length would be four four minus zero is Four and I minus one which is three and that would be the index of Whatever I'm currently looking on at so that let's say so these are the indexes zero one two three This would basically swap whatever length. I'm currently at Whatever value I'm currently currently at it's going to swap with whatever it is If I go to the halfway point so then now if I'm at two I could just swap to with With now I'm at index one I could swap index one with index two because index one is Four minus one is three Right and three minus one is two so that I'll swap this with two three and this would become two So yeah, if you were go to the halfway point and you swap the end last two values That would get you the same answer as you have Instead of reversing it Okay, so that would be much faster than the reverse method But so we're going to go through the halfway point and we're going to swap the two values so Temp is going to equal to whatever value I'm currently at So a at row At column and what am I going to do? I'm going to X or it by one and if you remember what X or does it's a bit wise operation that essentially changes the bits Flip the bits from one to zero zero to one So basically I'm going to change if this is zero. I will change it to one if it's one I'll change to zero. That's what this would do and then what I'm going to do is I'm going to swap Yeah, I'm going to swap with a at Row It would mean the number of columns minus one minus column X or this one So then that would get my if it's zero it becomes one one becomes zero and then I'll set my a at row at number of Columns minus one minus column is going to equal to 10 And how this is going to work how this works is like I told you you go to the middle and Then whatever in the beginning it's going to be become swap with the end And then you keep you go to you keep going Whatever index you're currently on you're going to swap it with whatever value at the end minus whatever can index you're on and That would basically get you your answer Let's see if it works Or what's the issue? No, I'm called it was declared though. Oh You have any questions, please leave a message not a message leave a comment down below and then I Could explain how how this code still works, but I hope you guys understand and Wow, yeah use less than a hundred percent of the C++ online submission Oh, it's so only faster than 71% hmm I'm wondering how I could make it even more fast. I Think you could not have a variable I guess but Yeah, whatever Yeah, Ray cops scribe. I'll check you guys later. Peace