 What's up guys, my name is Michael and welcome to my YouTube channel today We're going to go over another problem called n steps the number of steps So basically this problem is starting from point zero zero on a plane You write all the non negative numbers of zero one two shown in the figure and then this pattern basically continues So we have zero zero one one two four three three which is five Four four is eight five five nine six six twelve. Yeah, and here we have zero two zero Which is two three one is three four two is six five five three is seven six four is ten seven Five is eleven. Okay, so now our task each test case There's an X and Y representing them for each value in the input right the number written at that point or right None if they're so there's actually not that difficult of a problem I'm going to just explain how to do it now So guys if you were to label each point in like an X and Y axis So we would see that This line is actually if you were to solve this using a slope intercept form Y equals MX plus V rise overrun. So if you do like rise overrun You would actually get the line Y equals to X So this is the line Y equals X and then the second part of this has the exact same slope Except it's just shifted downwards. So if you were to keep continuing going downward and label the rest of these It's actually going to be Y equals X minus two So if we know that whatever coordinate that we pass in the first line and the second line Then that means we're here we can return a number, right? So a number only exists if it's in the first line and it's in the second line If it's not in any of these right then there's no number. So we know that For sure. So now now that we know about this Let's think about how you would solve this problem. Okay So if you look at the pattern 1 1 3 3 gives you 5 4 4 gives you 8 5 5 gives you 9 if you just write out every single number So if you do like 1 1 1 1 gives you 1 2 2 gives you 4 Now if you realize every single even number 2 2 is equal to 4 4 4 is equal to 8 6 6 is equal to 12 you just add the X and Y coordinate Right. So if they're both even you just add the coordinates together if it's an odd You realize that 1 1 is equal to 1 3 3 because 5 5 5 is equal to 9 you just add them up and subtract by 1, right? So if it's odd if it's even you just add them up if it's odd 1 1 gives you 1 because 1 plus 1 minus 1 is equal to 1, right? So that's the same 3 3 gives you 5 because 3 3 3 plus 3 minus 1 is equal to 5 and Then 5 5 gives you 9 because 5 plus 5 Minus 1 is equal to 9. Okay, so this is the first for the line y equals x Okay, now let's look at the second line y equals to x minus 2 If we look at here, we have 2 0 gives us 2 3 1 gives us 3 4 2 gives us 6 5 3 gives us 7 6 4 gives us 10 7 5 gives us 11 If we look at this line y equals x minus 2, it's the exact same thing, right? If it's even you just add up X and Y gives us 2 4 plus 2 gives you 6 6 plus 4 is 10. Yeah, and the odd values 3 and 1 Is you add them up and subtract by 1 5 and 3 3 plus 5 is 8 minus 1 is 7 6 plus 7 plus 5 is 12 minus 1 is equal to 11 So yeah, they both follow the same pattern exactly the same pattern So yeah, if you just want to Print the line Having a number at the point or write no number if there's none Basically, you would just plug the numbers in into both of these lines either X and Y Plug the numbers in Y equals X and Y equals X minus 2 If they if either of them work We know it has a number and then we just print out we check If it's both odd if it's odd we add them up and subtract one if they're both even then They are both we just add the X and Y values together Okay, so if the algorithm work will work something like this X Y equals X or Y equals X minus 2 Then it means it has a line right it means that this has a number then there's a number So then we just need to check the number, right? Otherwise, there's no number. So here if it falls in the line this or that Then we check in this case. We have to have another if statement if X and Y are both odd then we just print print X plus Y minus 1 otherwise if X and Y are even print X plus Y Okay, so that's basically a whole algorithm and now I'll actually type it up with you guys because I originally I did this in Java So now I'm gonna do it in C++. I'm gonna read in X and Y So this is gonna be my two values X and Y Okay, so now once I read X and Y. I just need to do check the thing. So we need to check if if they are if both lie in Y equals X or Y equals to X minus 2 so to do that. Let's just plug these values into Y equals X Or so if Y is equal to X That's gonna check the first part of why you equal to X or if Y is equal to X minus 2 Right, they'll they'll check if Y is equal to X minus 2, right? And that means that we have a number, right? Otherwise we have to have to print no number So that's what we do here So now when we have a number we need to check if they're both are even or if they're both are odd. So if one is One is odd. So if X is odd if X and mod 2 is not equal to 0 and Y Mod 2 is not equal to 0 That means they're both odd if they're both odd you just print out X plus Y minus 1 Okay, now we need to check if they are both even so if X mod 2 is equal to 0 and Y mod 2 is equal to 0 Then we're going to print out X plus Y And yeah, that's pretty much the whole algorithm Um Now what we're going to do is we're just going to print uh submit this So here, yeah number of steps. That's the moment of truth. Yeah, and it got accepted So, yeah, that's how you do this problem. It's actually not that difficult a number of steps It's pretty basic Um, it's just you pattern recognition. But yeah Ray com subscribe. I hope you guys enjoyed this video. I'll check you guys later