InferenceAI

Enter at least two characters to search.

Start free

Queens Placements

hardRecursion & Backtracking40 min50 XP

Return the number of ways to place n queens on an n×nn \times n chessboard so that no two share a row, column or diagonal.

Constraints

  • 1≤n≤101 \le n \le 10

Examples

EXAMPLE WALKTHROUGHtwo solutions

A guided view of the published example, not a trace of your code.

INPUT[4]
TASKqueens_placements()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: n = 4

Output: 2

Explanation: two solutions

Input: n = 1

Output: 1

Explanation: one queen

Input: n = 2

Output: 0

Explanation: impossible

Hint 1

Place one queen per row.

Hint 2

Track used columns and both diagonals (r - c and r + c) in sets for O(1) checks.

Topics

DSARecursion & BacktrackingPython