RK DSA Studio

N-Queens

Place n queens on an n×n board so no two share a row, column, or diagonal.

Remember: One per row; columns and diagonals stay free.

Example: A 4×4 board has 2 solutions.

Time: O(n × n!) bound. Auxiliary space: O(n).

Formula: Column c, diagonal r − c, diagonal r + c must be free

Choose n from 1 to 5. One queen per row. Sets track occupied columns and diagonals. All solutions are counted; playback shows rejection and undo. Stored solutions and trace snapshots are excluded from auxiliary space.

Complexities describe the underlying algorithm; tutorial snapshots and rendering can use additional resources.

The interactive workspace loads here when JavaScript is available. You can still browse the learning notes and links above without it.