 What's up, guys? Mike the Coder here. Today we're going to go over number spiral, which is the next problem on the CSES problem set. So essentially is they give us this number spiral, which is an infinite grid, where the top upper left square has the number one, and the first five of the number spiral looks like this. Now the question asks is, given the row and the column, find us the return the number for that. So essentially if you look at this, in the test cases, the row two, column three. Given row two, this is row two, column three, return eight. Here I'll show you guys in paint, actually, just show you guys what I mean. So essentially this is the grid, and if we were to label the rows and columns, they look like this. Like one, two, what is going on? Three, four, five, then row one, row two, row three, row four, then row five. So the input statement here is now given the row and the column, find the number for the given row and column. So here they're asking row two and column three. So what is row two? Row two is the second row here, the second row, and the column three. Second row, third column would give us eight. So that's what the output would be eight. Now given row one and column one, row one, column one would give us one, which it would output one. Now given row one, two, three, four, column two, row four, column two would give us 15. So it outputs 15. So that's essentially the gist of the problem statement. Right, given the row and column, try to find the number in this infinite grid. So based on this grid, if you want to figure this out, the easiest way is actually to find the pattern of this grid and see what the actual pattern is. So if you look at this one, two, then nine, ten, twenty-five, four, three, eight, eleven, twenty-four, so on and so forth. We have to find the pattern of what's going on in the grid. And see, so if you look at this from one to two, we're going this way, so that's one. And then two to three, we're going down this way. And then from three to four, we go down to the left side. And then from four to five, we go down again. Five to six, we go to the right. Six to seven, we go to the right. Seven to eight, go to the right. We go to the top. Eight to nine, we go to the top here. Nine to ten goes to the right. Ten to eleven, go down. We go to twelve, then thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twenty-one, twenty-two, twenty-three, twenty-four, twenty-five. Right, so it's like a spiral going right down, left, so on and so forth. So you could try to create like a large grid and then try to like create the spiral race yourself. But that also takes a lot of time because it would take a lot of time to create the grid and then look up the spiral. As if you look on this, the maximum number is ten to the ninth. So that's about more than ten to the ninth elements of the grid. So that takes a long time. So how do you do this problem? To do this problem, you have to come up with a math equation based on what you see in the grid and how you get there. So the first thing you could look at is try to come up with a math equation based on the row and the column. So the first thing you could see is the diagonals because that's probably the easiest one to figure out the math equation. So given the first diagonal is one-one, that gives us one. So if one-one gives us one, we could try to guess what that means. Maybe it's like multiplying one times one would give us one. So let's try that. So if we do one times one, so if I just take the row and multiply the column, I would get one times one to give us one. So that works for this one. Now let's say row two, column two. This gives us three. So that's definitely not one times one. That's definitely not two times two. So if I just take the row of two, multiply by the column of two, that gives us four, which is not three. It's not three. But if I want to get three, what can I do if I want to multiply row by column? So to get from three from four, I could just subtract one. So maybe it's row times column minus one, right? We don't know. That could be the case. But if it was the case, that'll give us three, right? That'll give us three here. Okay. So we could pretend maybe it's row times column minus one. I don't know. We don't know yet. Okay. Now let's do three times three. So three times three is nine. So three times three gives us nine. And that's definitely not seven. But how do I get from seven from nine? Right? Minus two, right? So maybe it's row times column minus two now. So now it's row times column minus one. Now it's row times column minus two. All right. Now let's look at four times four. Okay. Let's look at the next one. Next one is four times four. Okay. So if we're doing four times four, four times four is 16. So how do we get 13? Oh, maybe it's, if I were going to want to do four times four, it's 16. To get to 13 from 16, I would do minus three. So I would do minus three. So I'm going to do 16 minus three. So maybe it's, what is going on here? Why can I not write? Okay. Maybe it's row times column minus three then. Right? Maybe it's four times four. Minus three would give us 13. Right? Because in order to get from 16 to 13, I would do four times four minus three to give us 13. So let's look at five now. So for five times five. Five times five is 25. So how do I get from five times five is 25. Now how do I get 21 though? Simple. Minus four. Right? So five times five is 25 minus four would give us 21. So as you can see here, the pattern, if we rewrite it all again. So if row is equal to column, right? If my current row and the current column is equal, what is going on? Yeah, whoops. So if we were to rewrite this whole thing again. Okay. So if we do row times column, the first one we did row times column minus one. And that didn't work. Right? So first it was row times column minus zero. Then it was row times column minus one. Then it was row times column minus two. Then row times column minus three. Then row times column minus four. And then row times column minus five. But what am I seeing from this? Right? If you see row times column, I started row times column from one and one. Right? And that was minus zero. Then it was two times two. And that was minus one. Right? It was two times two. Then it was minus one. Then it was three times three. Then minus two. Then it was four times four. Minus three. Then it was five times five. Minus four. Then it was six times six. Minus five. And it was one times one minus zero. Right? So if we're just guessing what's going on here, to get this offset of what I'm doing here, how far is zero and one from two? Right? How far is one from two? What's simple? Minus one. Right? If I want to get two to one, I just minus one. If I want to get three to two, I just minus one. If I want to get four to three, I just have to minus one. If I want to get to five to four, I just have to minus one. If I want to get from six to five, I just have to minus one. So if row is equal to column, so if my row is equal to column, row is equal to column. Right? It'll just be row times row minus row minus one. Okay? This would be the math equation for the diagonal. Right? For the diagonal. So here the diagonals are this diagonal. Okay? One, two, three, four, and five. Okay? So that was the first math equation would be that. Row times row minus row minus one. Okay? All right. And now for the other ones. The other ones are a little more tricky, but we'll get there. All right, guys. Now we're going to go over the two different situations which will occur if we have row is greater than, which is when the row is greater than the column or when the column is greater than the row. So let's actually look at when row is greater than column. So when the row is greater than column, so remember before what we did, we figured out the diagonals, right? When row is equal to column. So we were able to do that. So now the two situations now is when row is greater than column or when column is greater than row. Column is greater than row. Okay? So when row is greater than column, let's say we have row is greater than column. So our row is equal to five and our column is equal to one. Okay? So row is five, this row, fifth row, and then column is equal to one. So that's this part, column one, which is this is going to give us 17. Okay? So let's think about it. If we want to find when row is greater than column, how do I get the number, right? How do I get the number 17? Well, it's simple. If we already know what the diagonal is, so let's say we know 21, all we have to do is subtract how far 21 is from the current row that we're on. So in this case we have 17, right? We need to subtract how far is 17 from 21. So that's one, two, three, four, right? So if row is greater than column, so row is greater than column here, for row is equal to five, column is equal to one. What we do is we take the diagonal, which is 21, and we minus four to get 17, right? So this is going to give us 17. Okay? You guys bear with me? You guys understand what I'm doing? Okay? Right? So in this case, if we have fifth row and first column, which we get 17, we need to take 21 minus four to get 17. Okay? But now let's say we have fifth row, but fifth row, but second column. Okay? Second column. So when we have fifth row, and now column is equal to two. So what is the fifth row and column equal to? That's 18, right? So how far is 18 from 21, right? So to do that, let's take 21 minus 18. That's about one, two, three. That's three. So if I take 21 minus three, that's going to get me 18. Okay? Okay. I hope you guys are starting to see a pattern here, but it's not that difficult to see what the difference of the pattern is. But let's continue in case if you don't see the pattern yet. So let's say I'm at row is equal to five and column is equal to three. So 19. Okay? How am I going to get 19 from this? From 21. Simple. It's two, right? 21 minus two would give us 19. Okay? So you guys should start seeing a pattern here. But yeah, we could continue going forward. But now let's say we have row is equal to three and column is equal to one. Okay? So row is equal to three. Now column is equal to one. So if row is three, this is this five. Row three column one is for this five. How do I get five from the diagonal? Right? The diagonal here. So if I know the diagonal, which is seven, how do I get five from seven? Well, it's about minus two. Okay? Right? So if I have row is equal to three and column is equal to one. If I have the diagonal, it would be seven minus two, equal to five. Right? So as you could see here, based on all the values that we currently have, what is the pattern you notice? 21 minus four gives us 17. Okay? 21 minus three gives us 18. Right? 21 minus two gives us 19. But where do we get this offset from? Minus two minus four minus two. Right? It's simple. It's the difference between the row and the column. Right? So if I take five minus two, that's going to give us this three. Right? Five minus three is going to give us this offset of this two. Right? So and then if I look at here at this four. Right? So essentially what we need to do here is that if row is greater than column, in this certain situation here, at least in this certain case, what we do is we're going to take the diagonal number here that we find and then we need to subtract by the difference between row and column. Okay? All right. So yeah. But, but, yeah. So the formula here, if I go back here, let me just take this. The formula here would just be, let me just scroll down and get rid of all this, is in our current situation from the current examples we've done, if row is greater than column, what we're going to do is we're going to take the diagonal, so the diagonal number, so the furthest one on the right, this diagonal number that we found on the current diagonal, we're going to minus and row minus column. Right? Okay? So that's this part. That's this current part. Okay? So this is, this is the only situation where row is greater than column. Okay? So this is, this is the only situation where we currently see. Okay? But wait a minute. This is not always correct. Right? Let's say I'm at row four and then column one. So let's say we're at row four and then column one. 16. Well 16, we're not actually, we're not subtracting from the offset. Right? What is 16? Well, the diagonal from 16 is 13. Right? So if I, how do I get from 16 to 13? Well, the diagonal of this diagonal is 13. Right? How do I get 16 to 13? Well, I have to add three to get from 16 to 13. Right? So when row is equal to four and column is equal to one, I need to add, I have to take diagonal and add. So how do I get from 16 to 13? Right? So I have to take 13 plus three. Right? I have to take 13 and add by some number. In this case it would be three to get to 16. Right? So row four, column one is 16 and the diagonal here is 13. Right? In order to get from 13 to 16, I actually have to add by three. Right? So in what situation do we see in this case? Right? Well, how do I get this three? Well, this three is the same thing as row minus column. Right? This three. So in this case, we have to add row minus column. Okay? We need to add row minus column. Right? So in what situation do we add row minus column? What situation do we subtract row minus column? Well, as you can see here, if I just look through the pattern of each of the rows, the odd numbers we have to subtract row minus column. As you can see, the odd numbers from diagonal of 21 decreases. Right? When the row is odd, for the fifth row, the row is odd. Right? It's decreasing. But the fourth row, if you look at the fourth row, it's actually increasing. Right? This fourth row is increasing. 13, 14, 15, 16. Right? And if we go to third row from the diagonal of seven, it's decreasing again. Seven minus one, six minus one, five. And then the second row, it's actually increasing. So we're actually increasing. We're adding from three to then to four. So the math equation is going to be, if I rewrite all this again, if the row is odd, if the row is odd, what we're going to do is, we're going to take diagonal, the diagonal number. Right? The diagonal number. Then we subtract row minus column. Right? Take diagonal. So odd number. If the row is odd. Subtract. Right? We're decreasing it. Otherwise, if the row is even, we take diagonal, diagonal number, and we plus row minus column. Right? So here we have four. Here we're increasing by four minus one, which is three. So we take 13 plus three. So we get to 16. Right? So that's this equation here. So yeah. So essentially here, if row is greater than column, if row is odd, we're going to take the diagonal number and we subtract row minus column. If row is even, we take the diagonal number and we plus row minus column. Right? This is the equation here. Okay. So yeah. And now let's actually look at when column is greater than row. So now we have these equations and we need to find when column is greater than row. Let me just decrease this. So we have these equations now. I believe, wow, it's really, really blurred out now. Yeah. Okay. So that was row is greater than column. Now we need to do when column is greater than row. Okay? So how do I do when column is greater than row? So in this case, let's say we're at the first row and then the fifth column. So row is equal to one and then column is equal to five. Okay? So in this case, it would be 25. Okay? Okay. So let's go over a few cases. So let's say now when column row is equal to two and then column is equal to five also. So here this one, 24. So how do we get from 25? First number, first row, fifth column, 25. So if we look at this and we subtract the same difference, we see that we actually see about the same thing. So first the diagonal here is 21. So this is 21. Right? The diagonal here is 21. So how do I get 25 from 21? Simple. I just have to do one, two, three, four. Right? I have to add four. So plus four, this will give me 25. Right? That will give me this number 25. Okay? And then let's say we're at row two, column five. So row two, column five. So this one, this is 20, 20, 24. How do I get from 21 to 24? Simple. I do 21. I have plus one, two, three. Plus three. So it will just give me 24. Right? And then let's say we have row three, column five. It will be 21 plus one, two, plus two. Okay? So as you can see here, based on this, when row is one and column is five, we are actually, as you can see here, we are actually increasing. Right? We're increasing the number from the diagonal of 21. And what is the offset that we're increasing by? Simple. So 21 plus four gets us 25. When row is one, column is five. Right? Now, when row is two, column is five. 21 plus three gets us 24. Right? And then when row is three and column is five, we do 21 plus two gives us 23. So essentially what we're doing is we're taking the diagonal and we're adding by column minus row. Right? The column minus row. So how do we get four here? We took column which is five minus row of one gives us four. Right? And here in the next one, for 21 plus three, to get three, we took column which is five minus two, which gives us three. So we did diagonal plus three gets us 24. And then for row is equal to three, column is equal to five. We do diagonal, which is 21 plus column minus three, which is two, and then equal to 23. Okay? So in this certain case, what we're doing is when column is greater than row, we take diagonal and we plus column minus row. Okay? So to do that, this is what the equations would look like. We just take diagonal, we plus column minus row. Okay? That's essentially what we're doing here. Okay? But is this always the case? Not necessarily. As you can see here, when it's odd, just like the previous situation, when it's odd, when row is equal to one and column is equal to five. Right? The columns are increasing when it's odd. Right? 21, 22, 23, 24, 25. Right? When row is equal to five. But when column is equal to four, so let's say row is one and column is equal to four. Right? This 10, this 10 is actually decreasing. Right? This 10 is decreasing from 13. It becomes 13, 12, 11, 10. And this is decreasing. Right? But as we can see here, how did we get from 13 to 10? We took 13 and we subtract three in order to get 10. Right? Subtract three in order to get this number 10. So row one, column four, gives us 10. So what is the offset that we're doing? We're still subtracting row minus column. Right? My bad. Column minus row. We're still doing column minus row. Right? So in this case, for columns, if the column is greater than row, and this one's even, we subtract. If it's odd, we add. Okay? So if we're going to go back here, in this case, for the when column is greater than row, if I were to rewrite it again. So if column is greater than row, if column is odd, right? If column is odd, then we're going to increase it. Right? We're going to take diagonal and we plus column minus row. Right? We plus column minus row. So here, yeah, so in order to get 25 here, when column is five and row is one, we actually added column minus row. Added four in order to get, added four to 21 to get 25. Right? And now if column is even, what do we do? So if row is one and column is four, this is 10, we subtract it. Right? We subtract one, two, three, subtract three. So we do diagonal minus column minus row. Okay? So I hope you guys understand what I'm doing here. Okay? So, yeah, so currently we have one, two, three, we have four equations right now. So four situations. One is when column is greater than row, if the column is odd, we're going to do diagonal plus C minus R, the difference between column minus row. And if the column is even, we do diagonal minus column minus row. Okay? Now, yeah. And then we have, if we were to summarize the other ones, the other ones was if column, when row is greater than column, I think it's the same thing, but it was switched. Right? If we were to go back to, it was like diagonal plus row minus column. If it was odd, if row was odd, right? And then if else it was minus, minus row minus column. Okay? Yeah. So that was when row is greater. So now we have these equations. So this is like from previous, previously we have four equations here. So now the only thing we need to do is figure out how to find diagonal. And we already know what diagonal is. Essentially, diagonal is just whichever is on the diagonal, right? Of this, this line, which we found before. So before, what we found before when diagonal is when row is equal to column, right? But now, when they're not equal, how do we find the diagonal? It's actually easy. It's simple. We just use the same equation as what we did before. But we need to find the maximum number between row and column. So let's say we're at here, 17 again. Let's say we're at 17 here. And row is equal to 5, and column is equal to 1. Okay? So to define the diagonal of 21, what we do is we're going to find the maximum number between row and column of 5 and 1, which is going to give us 5. Right? The max of 5 and 1 is going to give us 5. And what we're going to do is we're going to plug in this back into the equation of when row is equal to column, right? So that's 5 square minus 4, right? So this would be equal to 5 square minus 4, which is 25 minus 4, which would give us 21. So that gives us 21 here. Okay? So in order to find the diagonal, right, all you have to do is take the maximum number and plug it back to the original equation here that you had before, and that'll give you the diagonal. So yeah, once you have the diagonal number, you just plug in these four if statements, and then that'll give us your answer. So to look at the code, I'll show you guys what my code looks like. Okay, so here's what the code looks like. I have the test case t and r and c, which is row and column. And I see in the test cases, and then while t minus 5 is we're going to read in the row and column, and I pass in to solve, which is my function solve. Okay, so in solved, when row is equal to column, remember what we did before we just took row times row, and it subtracted r minus 1, which is row times row minus row minus 1. Okay, so that was the equation that we did first before in the previous part of the video. Now otherwise, what we're going to do is we're going to take the maximum number between row and column to find our diagonal number. So yeah, we do that. And then what we're going to do is we're going to plug this value, the maximum number, into our previous equation here of row times row minus row minus 1. So we're going to do val times val minus val minus 1. And that's going to give us the diagonal number. This value is going to give us the diagonal number. Now we're just going to subtract based on the offsets from our diagonal to get what the number we want. Okay, so if row is greater, if column is greater than row, what we're going to do is we check if column is even. So if column is even, then we do our diagonal. We subtract by column minus row. Okay, column minus row. So that'll give you your answer for that part. Otherwise, if column is odd, we're going to do diagonal plus equals to column minus row. Okay, so that's the first two statements, statements that we have here when column is greater than row. Okay, now the next one case is when row is greater than column. So when row is greater than column, what we're going to do is we're going to check if the row is even. So if the row is even, we're going to add the diagonal number plus equal to row minus column to get our current number that we want. Otherwise, we're going to subtract row minus column from our diagonal number. Okay, so those are the four if statements. And then in the end, we just print out the number that we want. So yeah, and that's essentially the gist of this problem. I hope you guys enjoy this video. Great, come subscribe. I'll check you guys later. Peace.