# Number of ways to place two queens on a N*N chess-board

Given an integer **N** denoting a **N * N** chess-board, the task is to count the number of ways to place two queens on the board such that, they do no attack each other.

**Examples:**

Input:N = 9

Output:2184

Explanation:

There are 2184 ways to place two queens on 9 * 9 chess-board.

Input:N = 3

Output:8

Explanation:

There are 8 ways to place two queens on 3 * 3 chess-board.

**Naive Approach:** A simple solution will be to choose two every possible position for the two queens on the N * N matrix and check that they are not in horizontal, vertical, positive diagonal or negative diagonal. If yes then increment the count by 1.

**Time Complexity:** O(N^{4})

**Efficient Approach:** The idea is to use combinations to compute the possible positions of the queens such that they do not attack each other. A useful observation is that it is quite easy to calculate the number of positions that a single queen attacks. That is –

Number of positions a queen attack = (N - 1) + (N - 1) + (D - 1) Here, // First N-1 denotes positions in horizontal direction // Second N-1 denotes positions in vertical direction // D = Number of positions in positive and negative diagonal

If we do not place the queen on the last row and the last column then the answer will simply be the number of positions to place in a chessboard of (N-1)*(N-1), whereas if we place in the last column and last row then possible positions for queens will be 2*N – 1 and attacking at 3*(N – 1) positions. Therefore, the possible positions for the other queen will be N^{2} – 3*(N-1) – 1. Finally, there are (N-1)*(N-2) combinations where both queens are on the last row and last column. Therefore, the recurrence relation will be –

Q(N) = Q(N-1) + (N^{2}-3*(N-1)-1)-(N-1)*(N-2) // By Induction Q(N) = (N^{4})/2 - 5*(N^{3})/3 + 3*(N^{2})/2 - N/3

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// number of ways to place two ` `// queens on the N * N chess board ` ` ` `#include <bits/stdc++.h> ` ` ` `#define ll long long ` `using` `namespace` `std; ` ` ` `// Function to find number of valid ` `// positions for two queens in the ` `// N * N chess board ` `ll possiblePositions(ll n) ` `{ ` ` ` `ll term1 = ` `pow` `(n, 4); ` ` ` `ll term2 = ` `pow` `(n, 3); ` ` ` `ll term3 = ` `pow` `(n, 2); ` ` ` `ll term4 = n / 3; ` ` ` `ll ans = (` `ceil` `)(term1) / 2 - ` ` ` `(` `ceil` `)(5 * term2) / 3 + ` ` ` `(` `ceil` `)(3 * term3) / 2 - term4; ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `ll n; ` ` ` `n = 3; ` ` ` ` ` `// Function Call ` ` ` `ll ans = possiblePositions(n); ` ` ` `cout << ans << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// number of ways to place two ` `// queens on the N * N chess board ` `class` `GFG{ ` ` ` `// Function to find number of valid ` `// positions for two queens in the ` `// N * N chess board ` `static` `double` `possiblePositions(` `double` `n) ` `{ ` ` ` `double` `term1 = Math.pow(n, ` `4` `); ` ` ` `double` `term2 = Math.pow(n, ` `3` `); ` ` ` `double` `term3 = Math.pow(n, ` `2` `); ` ` ` `double` `term4 = n / ` `3` `; ` ` ` `double` `ans = (Math.ceil(term1 / ` `2` `)) - ` ` ` `(Math.ceil(` `5` `* term2) / ` `3` `) + ` ` ` `(Math.ceil(` `3` `* term3) / ` `2` `) - term4; ` ` ` ` ` `return` `(` `long` `)ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `double` `n; ` ` ` `n = ` `3` `; ` ` ` ` ` `// Function Call ` ` ` `double` `ans = possiblePositions(n); ` ` ` `System.out.print(ans + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// number of ways to place two ` `// queens on the N * N chess board ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find number of valid ` `// positions for two queens in the ` `// N * N chess board ` `static` `double` `possiblePositions(` `double` `n) ` `{ ` ` ` `double` `term1 = Math.Pow(n, 4); ` ` ` `double` `term2 = Math.Pow(n, 3); ` ` ` `double` `term3 = Math.Pow(n, 2); ` ` ` `double` `term4 = n / 3; ` ` ` `double` `ans = (Math.Ceiling(term1 / 2)) - ` ` ` `(Math.Ceiling(5 * term2) / 3) + ` ` ` `(Math.Ceiling(3 * term3) / 2) - term4; ` ` ` ` ` `return` `(` `long` `)ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `double` `n; ` ` ` `n = 3; ` ` ` ` ` `// Function Call ` ` ` `double` `ans = possiblePositions(n); ` ` ` `Console.Write(ans + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Total number of ways to place X and Y at n places such that no two X are together
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Puzzle | Program to find number of squares in a chessboard
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Place Value of a given digit in a number
- Number of ways to split a binary number such that every part is divisible by 2
- Number of distinct ways to represent a number as sum of K unique primes
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count number of ways to divide a number in 4 parts
- Find the number of ways to divide number into four parts such that a = c and b = d
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Number of ways to get a given sum with n number of m-faced dices
- Ways to represent a number as a sum of 1's and 2's
- Count number of ways to get Odd Sum
- Number of ways of writing N as a sum of 4 squares
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to go from one point to another in a grid
- Number of ways to pair people
- Number of ways to reach the end of matrix with non-zero AND value
- Count ways to express a number as sum of exactly two numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.