Skip to content
RED SAGSkills

Count Queen Placements

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

Return how many different placements exist. Using bit masks for the occupied columns and diagonals makes the search fast.

Example 1

Input: n = 4
Output: 2

Example 2

Input: n = 1
Output: 1

Constraints

  • 1 <= n <= 10
Hint 1

Place one queen per row, recursing to the next row.

Hint 2

Track attacked columns and both diagonal directions as bit masks; shift the diagonal masks by one when moving to the next row.

Discussion 0

Join free to post in the discussion.

Good practice for: Game Developer, Backend Developer, Embedded Systems Engineer, AI Engineer (LLM & Generative AI), Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.