`{"id":"fc6db7ce-6896-4a1c-b36e-4bb018eda37a","name":"N Queens Using Bit","description":"1. You are given a number n, the size of a chess board.\r\n2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another.\r\n Note - Queens kill at distance in all 8 directions\r\n3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens\r\n Use sample input and output to get more idea.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is.\r\n Write recursive and not iterative logic. The purpose of the question is to aid learning recursion, branch and bound technique, bit manipulation \r\n and not test you.","inputFormat":"A number n","outputFormat":"Safe configurations of queens as suggested in sample output","constraints":"1 &lt;= n &lt;= 10","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<vector<bool>>& board, int row, int cols, int ndiag, int rdiag, string asf) {\n // write your code here\n }\n\n int main() {\n int n;\n cin>>n;\n vector<vector<bool>> board(n,vector<bool>(n, false));\n int cols = 0;\n int ndiag = 0;\n int rdiag = 0;\n solution(board, 0, cols, ndiag, rdiag, \"\");\n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(boolean[][] board, int row, int cols, int ndiag, int rdiag, String asf) {\r\n // write your code here\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n boolean[][] board = new boolean[n][n];\r\n int cols = 0;\r\n int ndiag = 0;\r\n int rdiag = 0;\r\n solution(board, 0, cols, ndiag, rdiag, \"\");\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4","sampleOutput":"0-1, 1-3, 2-0, 3-2, .\r\n0-2, 1-0, 2-3, 3-1, .\r\n","questionVideo":"https://www.youtube.com/embed/7qWsFBBTFRE?end=675","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"645a8370-1004-41f6-be50-731a9aa6a2ac","name":"N Queens Using Bit","slug":"n-queens-using-bit","type":1}],"next":{"id":"4e818815-dc21-4a70-9925-b78e3c0bec80","name":"N Queens Using Bits MCQ","type":0,"slug":"n-queens-using-bits-mcq"},"prev":{"id":"8f402e99-771b-4476-8f73-05268d66d7d6","name":"Sudoku Using Bit Manipulation","type":3,"slug":"sudoku-using-bit-manipulation"}}}`

# N Queens Using Bit

1. You are given a number n, the size of a chess board. 2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another. Note - Queens kill at distance in all 8 directions 3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens Use sample input and output to get more idea. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion, branch and bound technique, bit manipulation and not test you.

`{"id":"fc6db7ce-6896-4a1c-b36e-4bb018eda37a","name":"N Queens Using Bit","description":"1. You are given a number n, the size of a chess board.\r\n2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another.\r\n Note - Queens kill at distance in all 8 directions\r\n3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens\r\n Use sample input and output to get more idea.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is.\r\n Write recursive and not iterative logic. The purpose of the question is to aid learning recursion, branch and bound technique, bit manipulation \r\n and not test you.","inputFormat":"A number n","outputFormat":"Safe configurations of queens as suggested in sample output","constraints":"1 &lt;= n &lt;= 10","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<vector<bool>>& board, int row, int cols, int ndiag, int rdiag, string asf) {\n // write your code here\n }\n\n int main() {\n int n;\n cin>>n;\n vector<vector<bool>> board(n,vector<bool>(n, false));\n int cols = 0;\n int ndiag = 0;\n int rdiag = 0;\n solution(board, 0, cols, ndiag, rdiag, \"\");\n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(boolean[][] board, int row, int cols, int ndiag, int rdiag, String asf) {\r\n // write your code here\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n boolean[][] board = new boolean[n][n];\r\n int cols = 0;\r\n int ndiag = 0;\r\n int rdiag = 0;\r\n solution(board, 0, cols, ndiag, rdiag, \"\");\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"4","sampleOutput":"0-1, 1-3, 2-0, 3-2, .\r\n0-2, 1-0, 2-3, 3-1, .\r\n","questionVideo":"https://www.youtube.com/embed/7qWsFBBTFRE?end=675","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"645a8370-1004-41f6-be50-731a9aa6a2ac","name":"N Queens Using Bit","slug":"n-queens-using-bit","type":1}],"next":{"id":"4e818815-dc21-4a70-9925-b78e3c0bec80","name":"N Queens Using Bits MCQ","type":0,"slug":"n-queens-using-bits-mcq"},"prev":{"id":"8f402e99-771b-4476-8f73-05268d66d7d6","name":"Sudoku Using Bit Manipulation","type":3,"slug":"sudoku-using-bit-manipulation"}}}`

Editor

# N Queens Using Bit

medium

1. You are given a number n, the size of a chess board. 2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another. Note - Queens kill at distance in all 8 directions 3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens Use sample input and output to get more idea. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion, branch and bound technique, bit manipulation and not test you.

1 <= n <= 10

## Format

A number n

### Output

Safe configurations of queens as suggested in sample output

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4`

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0-1, 1-3, 2-0, 3-2, . 0-2, 1-0, 2-3, 3-1, . ```

Question Video

Discussions

Show Discussion

Related Resources