VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-ways-to-place-knights-moving-in-l-shape-in-chessboard/

⇱ Ways to place 2 Non-Attacking Knights in chessboard - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ways to place 2 Non-Attacking Knights in chessboard

Last Updated : 6 Jun, 2026

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 square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.

👁 Image

Examples:

Input: n = 2, m = 2
Output: 12
Explanation: The first Kniight can be placed in any of the 4 cells and the second knight can be be placed in any of the remaining 3 cells. For a Knight to attack. one dimension must be at least 3.

Input: n = 2, m = 3
Output: 26

[Naive Approach] Counting Valid Knight Moves – O(n * m) Time and O(1) Space

The idea is to count total ways to place two knights on the board and subtract the number of attacking pairs. For every cell, we calculate how many valid knight moves it can make (only in one direction set to avoid double counting).

  • Total ways two knights can be placed is (n*m)*(n*m - 1) [The first one at any square and the second on at any other square)
  • Traverse every cell and count valid knight moves using predefined directions
  • Each valid move contributes to attacking pairs count hence attacking positions increase by 2.
  • Return total pairs minus attacking pairs.

Output
26

[Optimal Approach] Using Mathematical Formula – O(1) Time and O(1) Space

The arrangements attacks where knight to move 2 steps in the horizontal direction and 1 step in the vertical. to 4 * (n - 2) * (m - 1) and similarly for 2 steps in the vertical direction and 1 step in the horizontal. Thus the answer will be Total possible arrangement - 4 * (n - 2) * (m - 1) - 4 * (n - 1) * (m - 2) which is (n*m)*(n*m - 1) - 4 * (n - 2) * (m - 1) - 4 * (n - 1) * (m - 2)

How does this work?

  • Consider knight to move 2 steps in the horizontal direction and 1 step in the vertical. So if we are at (i, j) after such moves we can reach at (i+2, j+1), (i+2, j-1), (i-2, j+1), (i-2, j-1). To have (i+2) inside the board we can have our positions 0 to n-3 i.e we have to leave the last two rows otherwise (i+2) will be out of the board. similarly for (i-2) range is possible if 2 to n-1.
  • Similarly for (j+1) range will be 0 to m-2, and for (j-1) range will be 1 to m-1 i.e one column less in each case.
  • So, arrangements in this case where attack possible equal to 4 * (n - 2) * (m - 1)
  • Similarly, if we consider two steps in vertical and one step in horizontal we will have one less row and two less col so that two knights can attack each other. We subtract this arrangement from total arrangements which is m * n * (n * n - 1). Hence the answer will be m * n * (m * n - 1) - 4 * (n - 2) * (m - 1) - 4 * (n - 1) * (m - 2)

Output
26
Comment
Article Tags:
Article Tags: