Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

N-Queens II

Hard
backtrackingrecursion

Count the number of distinct ways to place n queens on an n × n chessboard so that no two attack each other (no shared row, column, or diagonal).

Input: one line, the integer n. Output: the number of solutions.

Example 1
Input:
4
Output:
2
Example 2
Input:
1
Output:
1

Constraints

  • 1 ≤ n ≤ 9
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.