solulubeta
Sign in
Recursion & Memo / Choices and backtracking
02.08 · Problem

N-Queens: Count the Boards

Hard Backtracking with pruning LC 52 Pro

Place n queens on an n × n chessboard so that no two attack each other: no two share a row, a column or a diagonal. Return how many ways there are.

Examples

Example 1

Input:  n = 4
Output: 2
. Q . .      . . Q .
. . . Q      Q . . .
Q . . .      . . . Q
. . Q .      . Q . .
solulu Pro

Continue this problem with Pro

Inside: step-by-step hints · 9 hidden tests · reference solutions · code editor.

Lifetime ₹1,999 ₹999 · Launch offer
Mark this problem as solved when you are finished.

Item 19 of 44 · updated 2026-10-13