`{"id":"ffd69779-6c11-4f35-af0f-95171f95a9c3","name":"Solve Sudoku","description":"1. You are give a partially filled 9*9 2-D array(arr) which represents an incomplete sudoku state.\r\n2. You are required to assign the digits from 1 to 9 to the empty cells following some rules.\r\nRule 1 -> Digits from 1-9 must occur exactly once in each row.\r\nRule 2 -> Digits from 1-9 must occur exactly once in each column.\r\nRule 3 -> Digits from 1-9 must occur exactly once in each 3x3 sub-array of the given 2D array.\r\n\r\nAssumption -> The given Sudoku puzzle will have a single unique solution.","inputFormat":"9*9 integers ranging from 1 to 9.\r\n0 represents an empty cell.","outputFormat":"You have to print the solved sudoku.","constraints":"0 &lt;= arr[i][j] &lt;= 9","sampleCode":{"cpp":{"code":""},"java":{"code":"// import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void display(int[][] board){\r\n for(int i = 0; i < board.length; i++){\r\n for(int j = 0; j < board[0].length; j++){\r\n System.out.print(board[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n public static void solveSudoku(int[][] board, int i, int j) {\r\n // write yopur 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[][] arr = new int[9][9];\r\n for (int i = 0; i < 9; i++) {\r\n for (int j = 0; j < 9; j++) {\r\n arr[i][j] = scn.nextInt();\r\n }\r\n }\r\n\r\n solveSudoku(arr, 0, 0);\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"3 0 6 5 0 8 4 0 0\r\n5 2 0 0 0 0 0 0 0\r\n0 8 7 0 0 0 0 3 1\r\n0 0 3 0 1 0 0 8 0\r\n9 0 0 8 6 3 0 0 5\r\n0 5 0 0 9 0 6 0 0\r\n1 3 0 0 0 0 2 5 0\r\n0 0 0 0 0 0 0 7 4\r\n0 0 5 2 0 6 3 0 0","sampleOutput":"3 1 6 5 7 8 4 9 2 \r\n5 2 9 1 3 4 7 6 8 \r\n4 8 7 6 2 9 5 3 1 \r\n2 6 3 4 1 5 9 8 7 \r\n9 7 4 8 6 3 1 2 5 \r\n8 5 1 7 9 2 6 4 3 \r\n1 3 8 9 4 7 2 5 6 \r\n6 9 2 3 5 1 8 7 4 \r\n7 4 5 2 8 6 3 1 9 \r\n","questionVideo":"https://www.youtube.com/embed/uyetDh-DyDg?start=0&end=105","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":"07f3372d-141a-48bc-89c1-94426fed9e6f","name":"Solve Sudoku","slug":"solve-sudoku","type":1}],"next":{"id":"42f82b2f-c650-4be0-93e0-e43081dd422b","name":"Solve Sudoku MCQ","type":0,"slug":"solve-sudoku-mcq"},"prev":{"id":"46f278e4-1a53-4b86-b2e7-e3fb6b31c02a","name":"Gold Mine MCQ","type":0,"slug":"gold-mine-mcq"}}}`

# Solve Sudoku

1. You are give a partially filled 9*9 2-D array(arr) which represents an incomplete sudoku state. 2. You are required to assign the digits from 1 to 9 to the empty cells following some rules. Rule 1 -> Digits from 1-9 must occur exactly once in each row. Rule 2 -> Digits from 1-9 must occur exactly once in each column. Rule 3 -> Digits from 1-9 must occur exactly once in each 3x3 sub-array of the given 2D array. Assumption -> The given Sudoku puzzle will have a single unique solution.

`{"id":"ffd69779-6c11-4f35-af0f-95171f95a9c3","name":"Solve Sudoku","description":"1. You are give a partially filled 9*9 2-D array(arr) which represents an incomplete sudoku state.\r\n2. You are required to assign the digits from 1 to 9 to the empty cells following some rules.\r\nRule 1 -> Digits from 1-9 must occur exactly once in each row.\r\nRule 2 -> Digits from 1-9 must occur exactly once in each column.\r\nRule 3 -> Digits from 1-9 must occur exactly once in each 3x3 sub-array of the given 2D array.\r\n\r\nAssumption -> The given Sudoku puzzle will have a single unique solution.","inputFormat":"9*9 integers ranging from 1 to 9.\r\n0 represents an empty cell.","outputFormat":"You have to print the solved sudoku.","constraints":"0 &lt;= arr[i][j] &lt;= 9","sampleCode":{"cpp":{"code":""},"java":{"code":"// import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static void display(int[][] board){\r\n for(int i = 0; i < board.length; i++){\r\n for(int j = 0; j < board[0].length; j++){\r\n System.out.print(board[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n }\r\n\r\n public static void solveSudoku(int[][] board, int i, int j) {\r\n // write yopur 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[][] arr = new int[9][9];\r\n for (int i = 0; i < 9; i++) {\r\n for (int j = 0; j < 9; j++) {\r\n arr[i][j] = scn.nextInt();\r\n }\r\n }\r\n\r\n solveSudoku(arr, 0, 0);\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"3 0 6 5 0 8 4 0 0\r\n5 2 0 0 0 0 0 0 0\r\n0 8 7 0 0 0 0 3 1\r\n0 0 3 0 1 0 0 8 0\r\n9 0 0 8 6 3 0 0 5\r\n0 5 0 0 9 0 6 0 0\r\n1 3 0 0 0 0 2 5 0\r\n0 0 0 0 0 0 0 7 4\r\n0 0 5 2 0 6 3 0 0","sampleOutput":"3 1 6 5 7 8 4 9 2 \r\n5 2 9 1 3 4 7 6 8 \r\n4 8 7 6 2 9 5 3 1 \r\n2 6 3 4 1 5 9 8 7 \r\n9 7 4 8 6 3 1 2 5 \r\n8 5 1 7 9 2 6 4 3 \r\n1 3 8 9 4 7 2 5 6 \r\n6 9 2 3 5 1 8 7 4 \r\n7 4 5 2 8 6 3 1 9 \r\n","questionVideo":"https://www.youtube.com/embed/uyetDh-DyDg?start=0&end=105","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":"07f3372d-141a-48bc-89c1-94426fed9e6f","name":"Solve Sudoku","slug":"solve-sudoku","type":1}],"next":{"id":"42f82b2f-c650-4be0-93e0-e43081dd422b","name":"Solve Sudoku MCQ","type":0,"slug":"solve-sudoku-mcq"},"prev":{"id":"46f278e4-1a53-4b86-b2e7-e3fb6b31c02a","name":"Gold Mine MCQ","type":0,"slug":"gold-mine-mcq"}}}`

Editor

# Solve Sudoku

hard

1. You are give a partially filled 9*9 2-D array(arr) which represents an incomplete sudoku state. 2. You are required to assign the digits from 1 to 9 to the empty cells following some rules. Rule 1 -> Digits from 1-9 must occur exactly once in each row. Rule 2 -> Digits from 1-9 must occur exactly once in each column. Rule 3 -> Digits from 1-9 must occur exactly once in each 3x3 sub-array of the given 2D array. Assumption -> The given Sudoku puzzle will have a single unique solution.

## Constraints

0 <= arr[i][j] <= 9

## Format

### Input

9*9 integers ranging from 1 to 9. 0 represents an empty cell.

### Output

You have to print the solved sudoku.

## 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;}3 0 6 5 0 8 4 0 0 5 2 0 0 0 0 0 0 0 0 8 7 0 0 0 0 3 1 0 0 3 0 1 0 0 8 0 9 0 0 8 6 3 0 0 5 0 5 0 0 9 0 6 0 0 1 3 0 0 0 0 2 5 0 0 0 0 0 0 0 0 7 4 0 0 5 2 0 6 3 0 0```

### 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;}3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 ```

Question Video

Discussions

Show Discussion

Related Resources