 Alright guys, we're gonna go over then the the recent coding challenge 653 division 3 today And I'm gonna explain what I did to actually solve this for these two problems The first one I got ace. I got it wrong the first time Then I got a seed the second time because of division by zero I Think some something like that now There's a specific test case that caused me to not get it right anyway Here's the problem required remainder. So you have x y and n Which is three three integers and you need to find the maximum k which is between zero and n Such that k mod x is equal to y Okay, and then the mod is just like the remainder operator by the way, okay? In other words, you're given x y and n you need to find the maximum possible integer from zero to n That has a remainder y mod x, okay? All right, so this problem isn't actually that hard I'll explain what I did and then I hope you guys could understand it. All right guys, so Basically This is the problem given x y and n we need to find a k that is between zero and n such that K mod x is equal to y Okay, so how do you do this problem? well, first of all we could let's try to represent k in terms of x and y because If we we know we're given x y and n and if we could find a way to represent k in terms of x and y Then we could probably solve this problem, right? so However, how do I represent k in terms of x and y? So first of all, let's just Think about how the mod operator works again. So let's say I take seven mod by three And what is that so if I go three I try to divide seven by three right and then I figure out Oh, it's two three times two six seven by six one. I would get one Right a remainder of one because seven mod three is a remainder one So then how would I represent seven mod three three? How would I represent three and one in terms of seven? Right, it's simple. Just look at how you did your division earlier, right? Seven divided by three was gonna give you some number Okay, and that's some number you multiply by three add by your remainder. You'll get seven, right? So in this case seven divided by three gave us a Value of two and that that number was two two times three gave you six six plus one is seven Right, so seven is equal to three times two plus one right So in terms of modulus Basically, I'm dividing some number by another number and get that number if I multiply them again and add by the remainder I'll get back to my original number So how do I represent k in terms of x and y? Right because if I represent k in terms of x and y and we're given x and y we're gonna be able to solve for k So first of all K mod by x is equal to y. We know that right so k mod by x is equal to y So if I want to represent k in terms of x and y Right, I would have to do this It's gonna be the same thing as how we did it here What we have to do is there's some number when you divide K by x and let's just call that q right q is gonna be some number like q is it gonna equal to some number Any number whatever it is right right and then q multiplied by x Plus y is gonna give us k right q multiplied by x plus y is gonna give us k Right because q is some quotient. It's some number when you divide by x by k by x So that's why that's how we got this k is equal to x q plus y. Okay So now our issue is that okay? We have We have a x and y but we still have two variables, right? We don't know what q is We don't know what k is right so and now we have to try to represent k in terms of N and y right because we know that k is between 0 and n right k is between 0 and n So if we could represent k in terms of n Then we could figure out how to do this right So since k is between 0 and n Let's think let's say I have a number line One and ten right a number line one to ten Let's say my number is at this point Six so this is my number right I Tell you that this number is between one and ten right well. How would I get to this number six? Simple I would have to subtract starting from either one or ten From ten after subtract some number from my upper bound of ten by some amount In order to get to my number six so in this case I would have to subtract four from the upper upper bound ten to get me six or I could add Some amount of number from one so in this case plus five to get me to six So it's the same case in here K is between zero and n right so let's draw a number line of right here Zero and n and K is at this at this point zero and n right if I want to get to my number k How would I do it? Well, I have to add k from zero, but I don't know what k is Do you guys get it? I don't know what k is Right, so if k if I don't know what k is my only bet is to subtract some number from n to get to k So in this case, let's just call it w right if I subtract some number w From n I would get to k. So let's write that so Instead of instead of writing k. I'm gonna subtract n K is going to equal to n minus w where w is some distance from n Okay, so now you have n minus w. So now let's just plug n minus w into k into this equation Right so using substitution. So I'm gonna have n minus w is equal to x q plus y Okay, so now we have this Now we have this whole issue right here. Okay, and q plus We have n minus w is equal to x q plus y Okay, so now we now we know what this this side is Let's just rearrange it. Okay, we're gonna rearrange this So now I'm gonna have let's add w on the right side on both sides and subtract y from both sides So we're gonna have n minus y is going to equal to x q plus w Okay, so now at this point you should realize that You could actually get the number that you want well now since we have n and y We're good because we're given n and y if you have this side right and then all you have to do literally is just Divide by x Right using this equation this equation here and this equation here we could find we actually could find what q and w are now Right because the definition of modulus is if and division is that if you mod by something you You divide by that number and then you could you get the remainder Right, so now I could get I could get at my w now Right because if all I have to do is just take n minus y divide Have this number n minus y mod by x Now give me w right The reason why this give me w is because like X is some number that we're multiplying by q right and if you take n minus y and you mod by x That means you're dividing by x Right you divide by x and you get the remainder the remainder is going to be w Right based on the definition of modulus of what we saw here. So because if you have n minus y if you now divide you mod by x You're gonna get w Because n minus y and you divide by you yeah if you divide n minus y by x You're going to get some number Right, and then it's remainder is going to be w because we know n minus y is equal to x q plus w So that gives us this equation w is equal to n minus y mod by x Right, so now you have this this number now. We could just plug in all our numbers From here x y and n to get our w Once we have our w all we have to do is plug it back in here and then solve for q right, so if Once I have w I'm gonna plug this back in here and then solve for q and to solve for q I would just have to do n minus y minus my w then divide by x would give me q right um, but Yeah, and then once that occurs we could just do You guys just do Yeah You can solve for q and then after with it you solve for q you could also solve for k because now we know what k is k Is equal to n minus w? Right, so once you have your w you just plug it back into here and you can solve for your k So yeah, that's basically how I did this problem. So I'll show you guys the code now alright guys So this is the code just read in t number test cases read in Wild t minus minus read an x y and n and then the w remember w is just n minus y Mod by x so that gives us our w. How far it is from n Once we have that number We're gonna subtract that number from n and then yeah, that'll be your answer What I also did was I also checked if it's less than zero if it's less than zero then what you should do is add By n and the reason why you add by n is because that'll bring you back to the boundaries between zero and n Right if you if it's less than zero. So yeah, if I so what I did was if n minus w is less than zero I just add by n add another n to the right side to bring us back to the original boundaries But I don't even think they test this honestly. Yeah Otherwise, I just do print out n minus w right and minus w is how far we were from And so yeah, that's how you do this problem. Ray come subscribe. I'll check you guys later. Peace