N-Queens (4x4) Problem

N-Queens (4x4) Problem — ExecCode Hard DSA Practice

Solve the N-Queens (4x4) problem on ExecCode. Free online hard DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Examples

Input n = 1; Output [["Q"]]. Input n = 2; Output []. Input n = 4; Output [[".Q..", "...Q", "Q...", "..Q."], ["..Q.", "Q...", "...Q", ".Q.."]]

Constraints

Each row must contain exactly one queen. Each column must contain exactly one queen. No two queens can share the same diagonal.

Practice N-Queens (4x4) free on ExecCode. Browse DSA problems, topic map, and placement guides.