N Queens Problem Statement

You are provided with an integer 'N'. For an 'N' x 'N' chessboard, determine how to position 'N' queens such that no queen can attack another queen on the chessboard.

Explanation:

A queen can attack if it is positioned on the same row, column, or diagonal as another queen. Your task is to print all possible configurations.

Input:

N

Output:

Each line represents a single configuration.
Each configuration consists of 'N' * 'N' elements, printed row-wise with spaces in between. A position with a queen will have the value 1, otherwise, it will have the value 0.

Example:

Input:
N = 4
Output:
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0

Constraints:

  • 1 <= N <= 10
  • Time Limit: 1 sec

Note:

You do not need to print anything; the printing is already handled. Just implement the required function.
AnswerBot
4mo

The N Queens problem involves placing N queens on an N x N chessboard so that no two queens can attack each other.

  • Use backtracking to explore all possible configurations of queen placements.

  • Keep track...read more

Help your peers!
Select
Add answer anonymously...
ThoughtWorks SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits