Problem title
Difficulty
Avg time to solve

Longest Common Subsequence
Moderate
39 mins
N Queens
Hard
55 mins
Maximum sum of two non-overlapping subarrays of a given size
Moderate
15 mins
Encode The String
Easy
10 mins
Patternify
Easy
--
Longest Subarray Zero Sum
Easy
15 mins
Shortest path in an unweighted graph
Moderate
25 mins
Complete String
Hard
40 mins
Form a Triangle
Easy
15 mins
Longest Mountain Subarray
Easy
10 mins
39

N Queens

Difficulty: HARD
Contributed By
Avg. time to solve
55 min
Success Rate
35%

Problem Statement

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
Sample Input 1:
4
Sample Output 1:
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 
Explanation For Sample Input 1:
Output depicts two possible configurations of the chessboard for 4 queens.

The Chessboard matrix for the first configuration looks as follows:-

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

Queen contained cell is depicted by 1. As seen, No queen is in the same row, column, or diagonal as the other queens. Hence this is a valid configuration.
Sample Input 2:
3
Sample Output2:
      (Blank)
Explanation For Sample Input 2:
Since no possible configuration exists for 3 Queen's.The output remains Empty.
Reset Code
Full screen
copy-code
Console