CSES Solutions - Two Knights
Last Updated :
05 Apr, 2025
Given a number N, the task is to count for each K = 1,2 … N the number of ways two knights can be placed on a K X K chessboard so that they do not attack each other.
Examples:
Input: N = 2
Output: 0 6
Explanation:
- For a 1 X 1 chessboard, there is no possible way to place 2 knights on a 1 X 1 chessboard, ways = 0.
- For a 2 X 2 chessboard, there are 6 ways to place 2 knights on a 2 X 2 chessboard, ways = 6.

Input: N = 8
Output:0 6 28 96 252 550 1056 1848
Explanation:
- For a 1 X 1 chessboard, there is no possible way to place 2 knights on a 1 X 1 chessboard, ways = 0.
- For a 2 X 2 chessboard, there are 6 ways to place 2 knights on a 2 X 2 chessboard, ways = 6.
- For a 3 X 3 chessboard, there are 28 ways to place 2 knights on a 3 X 3 chessboard, ways = 28.
- For a 4 X 4 chessboard, there are 96 ways to place 2 knights on a 4 X 4 chessboard, ways = 96.
- For a 5 X 5 chessboard, there are 252 ways to place 2 knights on a 5 X 5 chessboard, ways = 252.
- For a 6 X 6 chessboard, there are 550 ways to place 2 knights on a 6 X 6 chessboard, ways = 550.
- For a 7 X 7 chessboard, there are 1056 ways to place 2 knights on a 7 X 7 chessboard, ways = 1056.
- For an 8 X 8 chessboard, there are 1848 ways to place 2 knights on an 8 X 8 chessboard, ways = 1848.
Approach: To solve the problem, follow the below idea:
Let's solve this problem for a particular K X K chessboard. Total number of squares in K X K chessboard is K2. So, number of ways the first knight can be placed is K2. Now the number of ways the second knight can be placed will be K2 - 1. This leads us to, the total number of ways the two knights can be placed in K X K chessboard is K2 * (K2-1)/2, we divide by 2 because both knights are identical. Now we need to subtract number of ways the two knights attack each other from above equation.
For this we need to observe that the two knights attack each other when they are in a 2 X 3 or 3 X 2 block. The number of ways two knights can attack each other in a 2 X 3 block is 2 and number of ways two knights can attack each other in a 3 X 2 block is 2. Refer to the below figure for better understanding:

Now, we just need to find the total number of 2 X 3 and 3 X 2 blocks in a K X K chessboard. In a K X K chessboard, the 2*3 blocks can be arranged in (K-1) rows (starting from the first row till the (K-1)-th row), and there are (K-2) ways to position them in columns (starting from the first column till the (K-2)-th column) which gives us (K-1)*(K-2) ways to place a 2 X 3 block in a K X K chessboard. Similarly, we can place a 3 X 2 block in (K - 1) * (K - 2) ways.
So, the total number of ways two knights can attack each other in a K X K chessboard will be sum of 2 * number of 2 X 3 blocks and 2 * number of 3 X 2 blocks, which is 4 * (K - 1) * (K - 2). It is every important to note that no 2 ways of attacking are same in the derived formula.
Hence the total number of ways Two Knights can be placed in a K X K chessboard such that they don't attack each other is:
(K2 * (K2 - 1)) / 2 - (4 * (K - 1) * (K - 2))
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
// Function to calculate and print the number of ways two
// knights can be placed on a K X K chessboard such that
// they do not attack each other
long calculateWays(int K) {
// Total number of ways two knights can be placed on
// the chessboard
long totalWays = ((long) K * K * (K * K - 1)) / 2;
// Number of ways two knights can attack each other
long attackingWays = 4 * (K - 1) * (K - 2);
// Number of ways two knights can be placed without
// attacking each other
long ans = totalWays - attackingWays;
// Return the result for the current chessboard size K
return ans;
}
// Driver Code
int main() {
// Input the value of N (size of the chessboard)
int N = 8;
// Iterate for all the K sized chessboard
for (int K = 1; K <= N; K++) {
cout << calculateWays(K) << " ";
}
return 0;
}
Java
public class TwoKnights {
// Function to calculate and print the number of ways two
// knights can be placed on a K X K chessboard such that
// they do not attack each other
static long calculateWays(int K) {
// Total number of ways two knights can be placed on
// the chessboard
long totalWays = ((long) K * K * (K * K - 1)) / 2;
// Number of ways two knights can attack each other
long attackingWays = 4 * (K - 1) * (K - 2);
// Number of ways two knights can be placed without
// attacking each other
long ans = totalWays - attackingWays;
// Return the result for the current chessboard size K
return ans;
}
// Driver Code
public static void main(String[] args) {
// Input the value of N (size of the chessboard)
int N = 8;
// Iterate for all the K sized chessboard
for (int K = 1; K <= N; K++) {
System.out.print(calculateWays(K) + " ");
}
}
}
// This code is contributed by shivamgupta310570
Python
# Function to calculate and print the number of ways two
# knights can be placed on a K X K chessboard such that
# they do not attack each other
def two_knights(K):
# Total number of ways two knights can be placed on
# the chessboard
total_ways = ((K * K) * ((K * K) - 1)) // 2
# Number of ways two knights can attack each other
attacking_ways = 4 * (K - 1) * (K - 2)
# Number of ways two knights can be placed without
# attacking each other
ans = total_ways - attacking_ways
# Return the result for the current chessboard size
return ans
# Driver Code
def main():
# Input the value of N (size of the chessboard)
N = 8
# Iterate for all the K sized chessboard
for K in range(1, N + 1):
print(two_knights(K), end=" ")
if __name__ == "__main__":
main()
JavaScript
// Function to calculate and print the number of ways two
// knights can be placed on a K X K chessboard such that
// they do not attack each other
function twoKnights(K) {
// Total number of ways two knights can be placed on
// the chessboard
let totalWays = (K * K * (K * K - 1)) / 2;
// Number of ways two knights can attack each other
let attackingWays = 4 * (K - 1) * (K - 2);
// Number of ways two knights can be placed without
// attacking each other
let ans = totalWays - attackingWays;
// Return the result for the current chessboard size
// K
return ans;
}
// Driver Code
function main() {
// Input the value of N (size of the chessboard)
let N = 8;
let result = "";
// Iterate for all the K sized chessboard
for (let K = 1; K <= N; K++) {
result += twoKnights(K) + " ";
}
// Print the result in a single line
console.log(result.trim());
}
main();
// This code is Contributed by Ayush Mishra
Output0 6 28 96 252 550 1056 1848
Time Complexity: O(N), where N is the number of rows or columns of chessboard.
Auxiliary Space:O(1)
Similar Reads
CSES Solutions - Coin Piles
You are given Q queries. In each query, you have two coin piles containing A and B coins. On each move, you can either remove one coin from the left pile and two coins from the right pile, or two coins from the left pile and one coin from the right pile. Your task is to efficiently find out if you c
7 min read
CSES Solutions - Another Game
There are n heaps of coins and two players who move alternately. On each move, a player selects some of the nonempty heaps and removes one coin from each heap. The player who removes the last coin wins the game. Your task is to find out who wins if both players play optimally. Examples: Input: N = 3
5 min read
Knightâs Tour Problem in C
Knight's Tour problem is a classic puzzle in which the goal is to move a knight on a chessboard such that the knight visits every square exactly once. The knight moves in an L-shape: two squares in one direction and then one square perpendicular to that or vice versa. In this article, we will learn
9 min read
Knightâs Tour Problem in C++
Knightâs Tour problem is a classic puzzle in which the goal is to move a knight on a chessboard such that the knight visits every square exactly once. The knight moves in an L-shape: two squares in one direction and then one square perpendicular to that or vice versa. In this article, we will learn
9 min read
CSES Solutions - Weird Algorithm
Consider an algorithm that takes as input a positive integer N. If N is even, the algorithm divides it by two, and if N is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until N is one. Your task is to simulate the execution of the algorithm for a given value of
4 min read
CSES Solutions - Chessboard and Queens
Your task is to place eight queens on a chessboard so that no two queens are attacking each other. As an additional challenge, each square is either free or reserved, and you can only place queens on the free squares. However, the reserved squares do not prevent queens from attacking each other. How
7 min read
Knight's tour for maximum points
Given a starting position in a 2D matrix representing a grid of points, the task is to find the minimum number of steps required for a knight to collect the maximum points, subject to the following conditions: The knight can move only as per the rules of a standard chess knight (i.e. two squares in
15+ min read
Count ways to place Knights moving in L shape in chessboard
Given two integers, N and M, denoting dimensions of a chessboard. The task is to count ways to place a black and a white knight on an N * M chessboard such that they do not attack each other the knights have to be placed on different squares. Note: A knight can move two squares horizontally and one
11 min read
Possible moves of knight
Given a chess board of dimension m * n. Find number of possible moves where knight can be moved on a chessboard from given position. If mat[i][j] = 1 then the block is filled by something else, otherwise empty. Assume that board consist of all pieces of same color, i.e., there are no blocks being at
7 min read
Place K-knights such that they do not attack each other
Given integers M, N and K, the task is to place K knights on an M*N chessboard such that they don't attack each other. The knights are expected to be placed on different squares on the board. A knight can move two squares vertically and one square horizontally or two squares horizontally and one squ
15+ min read