N Queens
You are given an integer 'N'. For a given 'N' x 'N' chessboard, find a way to place 'N' queens such that no queen can attack any other queen on the chessboard.
A queen can be killed when it lies in the same row, or same column, or the same diagonal of any of the other queens. You have to print all such configurations.
Input Format:
The first and the only line of input contains an integer 'N' representing the size of the chessboard and the number of queens.
Output Format:
Each line would be representing a single configuration.
Each configuration would contain 'N '* 'N' elements printed row-wise separated by spaces. The position where we can place the queen will have the value 1 rest will have the value 0.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'N' <= 10
Time Limit: 1sec
CodingNinjas
author
2y
Backtracking
- Instead of checking all the places on the chessboard, we can use backtracking and place queen row-wise or column-wise.
- Suppose we place queens row-wise, and we start with the very first row...read more
CodingNinjas
author
2y
Optimized Backtracking
- So instead of using a 2-D array to depict the chessboard, We can use three linear/boolean arrays of ‘N’ size and it will decrease the cost to check the cell is safe or not from O...read more
Help your peers!
Add answer anonymously...
Top ThoughtWorks SDE-2 interview questions & answers
Popular interview questions of SDE-2
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