N Queens Problem
Given an integer N
, find all possible placements of N
queens on an N x N
chessboard such that no two queens threaten each other.
Explanation:
A queen can attack another queen if they are in the same row, column, or diagonal. You need to find and print all valid configurations where queens do not attack each other.
Input:
Single integer 'N' representing the size of the chessboard and the number of queens.
Output:
Each line represents one valid chessboard configuration with 'N' queens. Each line contains 'N * N' elements (0 and 1) printed in a single row and separated by spaces. '1' indicates a queen's position, whereas '0' indicates an empty space.
Example:
Input:
4
Output:
0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0
0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0
Constraints:
1 <= N <= 10
- Time Limit: 1 sec
Note:
You do not need to print anything, as it is already handled. Just implement the function as specified.
Be the first one to answer
Add answer anonymously...
Top MagicBricks Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app