`{"id":"d6909331-5b20-4ad4-8520-368094ed6974","name":"Nknights Combinations - 2d As 1d - Knight Chooses","description":"1. You are given a number n, representing the size of a n * n chess board.\r\n2. You are required to calculate and print the combinations in which n knights can be placed on the \r\n n * n chess-board. \r\n3. No knight shall be able to kill another.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.","inputFormat":"A number n","outputFormat":"Check the sample output and question video","constraints":"1 &lt;= n &lt;= 5","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static boolean IsKnightSafe(boolean[][] chess, int i, int j) {\r\n // write your code here\r\n }\r\n\r\n public static void nknights(int kpsf, int tk, boolean[][] chess, int lcno) {\r\n if (kpsf == tk) {\r\n for (int row = 0; row < chess.length; row++) {\r\n for (int col = 0; col < chess.length; col++) {\r\n System.out.print(chess[row][col] ? \"k\\t\" : \"-\\t\");\r\n }\r\n System.out.println();\r\n }\r\n System.out.println();\r\n return;\r\n }\r\n\r\n for (int i = lcno + 1; i < chess.length * chess.length; i++) {\r\n int row = i / chess.length;\r\n int col = i % chess.length;\r\n\r\n if (chess[row][col] == false && IsKnightSafe(chess, row, col)) {\r\n chess[row][col] = true;\r\n nknights(kpsf + 1, tk, chess, row * chess.length + col);\r\n chess[row][col] = false;\r\n }\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n boolean[][] chess = new boolean[n][n];\r\n\r\n nknights(0, n, chess, -1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"2","sampleOutput":"k\tk\t\r\n-\t-\t\r\n\r\nk\t-\t\r\nk\t-\t\r\n\r\nk\t-\t\r\n-\tk\t\r\n\r\n-\tk\t\r\nk\t-\t\r\n\r\n-\tk\t\r\n-\tk\t\r\n\r\n-\t-\t\r\nk\tk\t\r\n\r\n","questionVideo":"https://www.youtube.com/embed/XSlMg_VUfwk?end=275","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":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"811e67eb-4ea8-414e-88a3-b5f2d5cfaf5f","name":"Nknights Combinations - 2d As 1d - Knight Chooses","slug":"nknights-combinations-2d-as-1d-knight-chooses","type":1}],"next":{"id":"8b10c00d-51a5-4439-882a-8e46ea4d730b","name":"Nknights Combinations - 2d As 1d - Knight Chooses MCQ","type":0,"slug":"nknights-combinations-2d-as-1d-knight-chooses-mcq"},"prev":{"id":"9f3de956-c31f-4182-afa5-7ca9edaa24ee","name":"Nqueens Combinations - 2d As 1d - Queen Chooses MCQ","type":0,"slug":"nqueens-combinations-2d-as-1d-queen-chooses-mcq"}}}`

# Nknights Combinations - 2d As 1d - Knight Chooses

1. You are given a number n, representing the size of a n * n chess board. 2. You are required to calculate and print the combinations in which n knights can be placed on the n * n chess-board. 3. No knight shall be able to kill another. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

`{"id":"d6909331-5b20-4ad4-8520-368094ed6974","name":"Nknights Combinations - 2d As 1d - Knight Chooses","description":"1. You are given a number n, representing the size of a n * n chess board.\r\n2. You are required to calculate and print the combinations in which n knights can be placed on the \r\n n * n chess-board. \r\n3. No knight shall be able to kill another.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.","inputFormat":"A number n","outputFormat":"Check the sample output and question video","constraints":"1 &lt;= n &lt;= 5","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static boolean IsKnightSafe(boolean[][] chess, int i, int j) {\r\n // write your code here\r\n }\r\n\r\n public static void nknights(int kpsf, int tk, boolean[][] chess, int lcno) {\r\n if (kpsf == tk) {\r\n for (int row = 0; row < chess.length; row++) {\r\n for (int col = 0; col < chess.length; col++) {\r\n System.out.print(chess[row][col] ? \"k\\t\" : \"-\\t\");\r\n }\r\n System.out.println();\r\n }\r\n System.out.println();\r\n return;\r\n }\r\n\r\n for (int i = lcno + 1; i < chess.length * chess.length; i++) {\r\n int row = i / chess.length;\r\n int col = i % chess.length;\r\n\r\n if (chess[row][col] == false && IsKnightSafe(chess, row, col)) {\r\n chess[row][col] = true;\r\n nknights(kpsf + 1, tk, chess, row * chess.length + col);\r\n chess[row][col] = false;\r\n }\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n boolean[][] chess = new boolean[n][n];\r\n\r\n nknights(0, n, chess, -1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"2","sampleOutput":"k\tk\t\r\n-\t-\t\r\n\r\nk\t-\t\r\nk\t-\t\r\n\r\nk\t-\t\r\n-\tk\t\r\n\r\n-\tk\t\r\nk\t-\t\r\n\r\n-\tk\t\r\n-\tk\t\r\n\r\n-\t-\t\r\nk\tk\t\r\n\r\n","questionVideo":"https://www.youtube.com/embed/XSlMg_VUfwk?end=275","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":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"811e67eb-4ea8-414e-88a3-b5f2d5cfaf5f","name":"Nknights Combinations - 2d As 1d - Knight Chooses","slug":"nknights-combinations-2d-as-1d-knight-chooses","type":1}],"next":{"id":"8b10c00d-51a5-4439-882a-8e46ea4d730b","name":"Nknights Combinations - 2d As 1d - Knight Chooses MCQ","type":0,"slug":"nknights-combinations-2d-as-1d-knight-chooses-mcq"},"prev":{"id":"9f3de956-c31f-4182-afa5-7ca9edaa24ee","name":"Nqueens Combinations - 2d As 1d - Queen Chooses MCQ","type":0,"slug":"nqueens-combinations-2d-as-1d-queen-chooses-mcq"}}}`

Editor

# Nknights Combinations - 2d As 1d - Knight Chooses

medium

1. You are given a number n, representing the size of a n * n chess board. 2. You are required to calculate and print the combinations in which n knights can be placed on the n * n chess-board. 3. No knight shall be able to kill another. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

1 <= n <= 5

## Format

A number n

### Output

Check the sample output and question video

## 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;}2`

### 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;}k k - - k - k - k - - k - k k - - k - k - - k k ```

Question Video

Discussions

Show Discussion

Related Resources