 What's up, guys? Mike, the coder here. Today we are going to go over this problem called two nights. So what is this problem? Your task is to count for k equal to 1 up to n. The number of ways two nights can be placed on a k by k board such that they do not attack each other. So what does that mean? Well, before you even go start doing this problem, you have to know how nights move. So how nights move is that they could only move in L shape. So let's say I have a night in the center of this board. A night can only move in an L shape. So it means that I could only move up to the left one or up to the right one. So an L shape I can move is like here and then here. So then I can move to this spot or I can move up to the right one. So up here, so I can move to this spot. And I can do the same to the bottom. So down two to the right one, up here, like this way. Down two to the left one and this way. And I also can move up one and to the left two. So I can move another L shape because you can move up one to the left two, so right here. Up one to the right two, up here. And then down one to the right two, right here. Down one to the left two, right here. So that's basically the different ways I could put it. And if I were to put a star on each of these ways, it'll be here, it'll be here, it'll be here, it'll be here, it'll be here, and then it'll be here, it'll be here. Because this is how a knight moves. Knights move only in L shapes, right? They only can move L shapes. So now our task is that given a K by K board, we need to count the number of ways two knights can be placed on this board, such that they do not attack each other. So before we even begin, let's just assume that these two knights are just pawns. So what I mean is that they can only move straight. And the reason why is because if they're just like regular pieces, so let's say they're just like regular pieces, any pieces. So let's think about this. So let's say I have this board here, and it's a one, two, three, four, five. So in our case, the size of the board would be K equal to five. Because in the problem statement they said K by K board, so in this case, actually it would be N equal to five. My bad, the board would be N. The board would be N. So in this case it would be N is equal to five. The board is equal to five. And let's say we just want to place any piece into this board. Well, how many different ways can I place it? So let's say I just want to do one piece. So I put a piece here. Well, okay, that's pretty good. So this is one place I could put it. But how many different ways can I put it? So I could put a piece here. But I also could put a piece here. These are random pieces. And I can put another piece here, another piece here, this piece here, this piece here, this piece here. here. Any of these number pieces I could put them, right? If I just put one piece here. So what is a total number of ways if I just want to put a piece? It's simple, right? It'll just be the length times width of the board, right? Because that's all be the total number of ways I could put one piece, right? Because if I just do 1, 2, 3, 4, 5, 6, I would just count them all up. It'll just be, I don't know, 25. So it would just be n squared, right? So if I just want to put one piece, right? If I just want to put one piece, if I just want to put one piece, it would be n squared. So it would just be n squared, okay? Because I just want to put one piece into this board, okay? But now let's say I want to put two pieces, right? So this one is not one piece. This is one piece, one piece. Assuming this is not an eye, it's just like a regular chest piece, right? It'll be n squared because I have like n ways to do here and then n ways to do here, right? So it would be n times n which would be n squared. So this is one piece. One piece would be n squared. But now let's assume I want to do two pieces, okay? Well if I place one piece here, right? How many different ways can I put the second piece? Well, if I want to place a second piece and I could switch to a color red, well I could put it here, right? I could put it here, right? I hope you guys can see this. Maybe I'll put it like orange here. Maybe gray. I'll see something that's pretty hard to see. Yeah, okay. I'll find I'll put green, green, right? So if I put one piece here for the first piece, how many different ways can I put it? Well, I could put one here, right? Or I could put another one here, right? I could put it either this one or this one, right? Or the second piece I could put it here. Or I could put another here, or I could put another here, right? And then just count what? This one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, this one, If I place the first one here, I had 24 ways to put the second piece. So in this case it would be, if I place the first piece here, I would have 24 pieces to put the second piece. So it would just be n square minus one, right? If I place the first piece here. But remember, we don't have, it's not necessary that we have to place the first piece here, right? You could place a piece here, I also, or you could place it here, or you could place it here, or you could place it here. And so on and so on and so forth, right? So if you want to count the number of ways you could put the first piece, right? There's n square to put the first piece, right? There's n square to put the first piece, right? So if I place the first one here, then there's that many ways to put it, right? And then there's n square minus one to put the second piece, right? Because if I had, if I place the first piece here, the second piece, there's one column less to put the second piece, right? So that's why I have to do the total number of squares, which is n square, and subtract by the first one, because then I'll be, that'd be the number of ways to put it, right? But think about it, if I just do n square times n square minus one, I'm actually double counting, right? I'm actually double counting, because what's happened is if I place the first piece here, imagine if I place the first piece, so n square minus one here. Imagine I place the first piece here and the second piece here, right? And then I just go on and so on and so forth, right? So on and so forth until I get to the end, right? So, and then imagine if I put the second piece here. So let's say I put the second piece here. So let's say I put the second piece here. I mean, the first piece here now. Well, I just double counted this one, right? I double counted this one, right? Like if I had placed the first piece here and then I placed the number of ways to put the second piece would be here, here, here, here, here, here, so on and so forth, right? And then so on and so forth keep going, right? And if I place this, the, now what if I place the first piece here now? Well, how many different ways could you place the second piece? It'll be here, here, here, here, here, like my bad, I'll put this one here, right? So here, it would be here, here, here, here, here, here, I'll put it in purple. So you guys see this one here, here, here, here, here, here, here, here, here, here, here, here, here, here. So we basically just double counted this one, the first two ways to do it, right? We double counted it because if I place the first piece here and then that will be n, n square minus one ways to put the second piece. And then what if I place the first piece here? Then it would be n square minus one to put the second piece also. But I just double counted this first one. So out of all the ways to do the first piece, right? If I want to put all the ways to put the first piece, the equation would be, the equation to do this, which I'm gonna have to come back here. Let me just redo everything. The equation would be n square times n square minus one, so it would be n square. So our equation for putting two pieces, assuming they're just regular ponds, it would be n square times n square minus one divided by two, okay? So this would be the equation, right? Just putting two pieces because otherwise I would have had double counted twice, right? You saw how we double counted the first way to do it this way. And we could have kept double counting. So that's why the answer would be n square times n square minus one over two. So this is like just placing two random pieces, right? These are just two random pieces. So you got to think of another way you could think about it is like, there's a total of n square pieces on this board, right? And the total number of ways to choose two pieces in it would be n square c2, right? This is like a second way to think about it. It would be n square c2, which would also, would just get you n times, n square times n square minus one divided by two, okay? That's another way to think about it. But yeah, I just wanted to show you like visually what it meant, why it's n square times n square minus one over two, okay? So this is the first part of the placing two random pieces on a board. So now what we're going to do is we are going to subtract by the ways that the two knights can attack each other. Because this is the total number of ways we take placing two pieces on the board, just two random pieces on the board. And now we just have to subtract the different ways where a knight, two knights can attack each other because that'll be your answer, essentially, okay? So now that we have the total number of ways two knights could be placed on a board, now all we have to do is subtract by the number of ways two knights do attack each other. So two knights do attack each other. So when do two knights attack each other? So if we think about it, let's say we have a knight here, right? So a knight can attack basically diagonal, right? So like in, not diagonal, L shape, in L shape. So it could be up here and then up one to the right here, up one to the right down one here, up here down one here. And then so on and so forth, right? It would be the diagonals, right? The L shapes going one and then to the right two or down one to the left two, right? So which places can I not put the knight? Well, simple, if I have the knight placed here, right? I cannot put the knight here and I cannot put the knight here and I cannot put the knight here, right? So what do we see here? What do we see in this picture? Well, if you were to cut the board around the locations where you cannot place it, you would see a pattern, right? If I had a knight here and then I can't place it here, right? But here, if I have a knight here, right? I also cannot place it here and I also cannot place it here, right? So let's just like, let's just like color in the areas where we cannot place it. So here, this is, I cannot place it here, right? But I'm going to just shade in those, the grid from the whole grid on the parts where I cannot place it. So here, I can't place it here. So I'm just, let's just shade in this part, okay? I'm just going to shade in here this part, all the surrounding areas, okay? I can't place it here, right? I'll just shade it. Well, I also cannot place it here. So I'm just going to shade in this part. Let's do a different color. Let's do it orange. So let's just shade it, maybe blue. Blue, blue, blue, blue is better. So I'll just shade in this part. I can't place it here, right? I can't place it here. So I'm going to shade in this part also. But I also can't place it here, right? Because if I have a knight here, I can't place a knight here. I'll attack this one. So I'll shade in this part also. So I'll shade in this part. So what do you see here after the, after shading in? It's simple. Essentially is that the, in rectangles of two by three, or three by two, the two, if you place a knight in those rectangles, they're going to attack each other, right? So if you could, if I have a knight here, and then if I have another knight here, these two knights are going to attack each other, because there is a rectangle of two by three here, right? There's a rectangle two by three of this part. This part is a rectangle here. It's two by three. And these two are going to attack each other. Also, if I have a knight here, right? Same here. This, this knight attacking this knight. If I place a knight here, and a knight here, these two knights are going to attack each other, because this is also in another rectangle of two by three, right? Two by three here. So this is also another rectangle of two by three, right? So that's why these two are also going to attack each other. But it's not just two by three. It's also three by two. Like if I have a rectangle of three by two here, so in this case of there's a knight here, right? There's a knight here. And I can't place a knight here, because there's a three by two here. There's a rectangle of three by two. So there's one, two, three. Three places here. And then another one here to the right here. It's a three by two here. So it's this rectangle. Hope you guys could see this rectangle, right? So essentially is that we just have to count how many two by three rectangles are in the grid and how many three by two rectangles are in the grid. And then we just subtract that from the total number of pieces, of two pieces you can place in the grid. And then that'll just be your answer, okay? So yeah, so the first part, how many two by threes are in a grid, okay? How many two by threes are in a grid? So let me just completely get rid of this again. And then we can start over. Let's count out how many two by threes are in a grid. And then we're going to be on our way. And then we add up how many three by two rectangles in our grid and so on and so forth. Okay, so how many two by threes are in this grid? So here I have a grid of n is equal to five, right? So it's a five by five grid. So how many two by three rectangles? Well, there's a two by three rectangle here, right? This one, this one, right? So there's one here, this one. And there's also one here, right? If I could count this one, so there's two. And there's another one here, right? That's three. And I also could count, keep counting. There's one here, four. And I could count another one here, five, six. Count another one here, right? And seven, count one here, eight. Count one here, nine. Count one here, 10. Count one here, 11. Count one here, and then 12. Count one here, okay? So what we notice here is that we just count. If I want to write a math equation regarding how many different rectangles there are, we just have to count like how many per row and then multiply by how many per column, okay? So here we have one here, two, three. We have three here. So there's three rectangles per this row. So there's three rectangles. If I just count like this three, right? Two. There's one, two, three, right? So there's three here. So as per each row, there's about three rectangles. And then how many per column? So there's a, let's see, how many columns are there? So one, two, three. Let's see. Then one. So there's one, two, three, four. Okay? So there's four. Four of these giant rectangles per column. So like one, two, three, and then four. Okay? So the answer would be three times four. So, yeah, so the answer would be three times four. So based on our n equal to five and three by times four, we could write a math equation generalizing when for all n. So in this case, it would just be, what do you see a pattern between this five and this three? Right? So three is actually just five minus two, right? So three is five minus two. And then four is just five minus one. So here it would be five minus two times four is five minus one. So five minus one, right? So in our n is equal to five, right? Our n is equal to five. So for generalizing this, it would just be n minus two times n minus one, right? Right? It would be n minus two times n minus one. So that'll be the math equation for how many rectangles, two by three rectangles for an n by n grid. So yeah, so this equation would be n minus two times n minus one. So here it would be n minus two times n minus one. So this would be the math equation for number of two by three rectangles in a grid, okay? So now we have to think about the number of three by two rectangles, right? We have to add this one add to the number of three by two rectangles, and then we just subtract it from our total number of two pieces we could place on our board and that'll be our answer, okay? So here, now that we have this, we're going to find how many three by two rectangles are on the board. So let's count that, okay? So I'm just going to erase this whole thing and yeah. So before we lose everything, so here remember our total was, our total was n square times n minus one, n square minus one over two, right? This is our total. And then we have to subtract the number of two by three rectangles and then also the number of three by two rectangles, right? So we're going to subtract, and our math equation was n minus two times n minus one, right? n by two times n minus one. So this is a two by three rectangles. This is a number of two by three rectangles, okay? So now let's count the number of three by two rectangles now, okay? So I'm going to erase this and I'll paste this here. So how do you count the number of three by two rectangles? Simple, same thing, go row by column. Remember this is n equal to five, five by five, right? Simple, go row by column and then just count how many per row and then generalize it for how many per column, okay? So here we have three by two, three by two. So this is we have one here, another here, two, another here, three, another here, four, okay? So this is like four in one row. And let's see how many columns we have. So we have one here, let's see. So we have one here, two, and then three, okay? So there's one, two, and then three. So there's four of them in one row and then there's three per column, right? Three per column. So the answer would be four times three. So if we want to generalize it, remember our n is equal to five. We're going to need to generalize these two based on n equal to five. So what is four representing in terms of five? It's five minus one. And what is three representing in terms of five? It's five minus two. So this would be our answer, right? Four is equal to five minus one. So four is equal to five minus one and three is equal to five minus two. So then now let's replace our five with n. So it's going to be n minus one times n minus two, okay? Easy. So now in the end, we're just going to have the total equation would just be n square times n square minus one over two, right? Minus here would be n minus two times n minus one plus n minus one times n minus two, okay? I actually forgot to mention one thing is that in each rectangle, there's two ways to place a knight. So if I had a knight here, I could place a knight here, attack a knight here, right? So these are the two ways you could place a knight to attack each other. So I could put here and attacking here, right? Or I could put a knight here, attacking a knight here, right? This one and this one or this one and this one, right? So that's why there's two ways to attack a knight for each square. So our answer would actually be two times this plus two times this, right? These are both subtracted. So it would be n square times n square minus one over two minus two times n minus two times n minus one plus two times n minus one times n minus two. So if we were to simplify this equation on the right, two times n minus two times n minus one plus two times n minus one times n minus two. And then if you were just... Because these are the same coefficients, if you add them up, they're just equal to four times n minus one times n minus two, right? Because they're the exact same coefficients. So yeah, our answer would be n square times n square minus one over two minus four times n minus one times n minus two. So if we were to rewrite this whole thing, if we were to rewrite this whole thing, it would be n square times n square minus one over two minus four times n minus one times n minus two, okay? So yeah, this would be the answer. Hope you guys enjoyed this video. I'm going to code it up and actually I'll just show you guys the code right now because it's not that hard. So here we have submissions. Yeah, so here basically I just read in n here and I call solved and then I'm going to loop through from k equal to one up to n because they wanted it for all values from k to one to n, right? That's what they wanted in the problem statement. So and then I just plug into the formula. I do k times k times k minus one over two, so this is k square times k square minus one over two and then I just minus four times k minus one times k minus two and then I do that for each of them for all the values of k from one to n because the problem statement for some reason they wanted to do from k equal to one to n, right? k equal to one n on one by k by k board, okay? So yeah, so you had to loop from one to n and then given n you had to plug in the equation for each of the values of k. So yeah, I could show you guys the code again. So yeah, I just plug it in k square times k square minus one over two minus four times k minus one times k minus two and loop from k one, loop from k one up to n, including n. And yeah, and the main is c and n and I just call solved. So yeah, this is just the code. I hope you guys enjoyed this video. If you want to subscribe, I'll check you guys later. Peace.