1. Declare a 2D Array with dimensions of 9 x 9.
Implement the following operations on this array:
i) Search ii) Traversal iii) Sum of all elements iv) Insertion v) Deletion.
Use the learned concept of this practical to solve Su-Do-Ku Puzzle whose statement is:
Su-Do-Ku Puzzle – A Sudoku is a problem where there are is an incomplete 9 x 9 table of numbers which must be
filled according to several rules:
✓ Within any of the 9 individual 3 x 3 boxes, each of the numbers 1 to 9 must be found.
✓ Within any column of the 9 x 9 grid, each of the numbers 1 to 9 must be found.
✓ Within any row of the 9 x 9 grid, each of the numbers 1 to 9 must be found.
Traversal in 2D(9*9) arrays
public class Traversal9x9 {
public static void main(String[] args) {
int[][] arr = new int[9][9];
int val = 1;
// Fill with values 1 to 81
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
arr[i][j] = val++;
}
}
System.out.println("Traversal of 9x9 Array:");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println("Number of rows: " + arr.length);
System.out.println("Number of columns: " + arr[0].length);
}
}
Insertion in 2D Array (9*9)
public class Insertion9x9 {
public static void main(String[] args) {
int[][] arr = new int[9][9];
int row = 4, col = 5, value = 99;
arr[row][col] = value;
System.out.println("After Insertion of " + value + " at (" + row + "," + col + "):");
printMatrix(arr);
}
static void printMatrix(int[][] arr) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Deletion in 2D Array (9*9)
public class Deletion9x9 {
public static void main(String[] args) {
int[][] arr = new int[9][9];
// Pre-fill with numbers
int val = 1;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
arr[i][j] = val++;
}
}
int row = 2, col = 3;
System.out.println("Before Deletion:");
printMatrix(arr);
System.out.println("\nDeleting " + arr[row][col] + " at (" + row + "," + col + ")");
arr[row][col] = 0;
System.out.println("\nAfter Deletion:");
printMatrix(arr);
}
static void printMatrix(int[][] arr) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Linear Search 2D Array (9*9)
public class Search9x9 {
public static void main(String[] args) {
int[][] arr = new int[9][9];
int val = 1;
// Fill 1 to 81
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
arr[i][j] = val++;
}
}
int key = 50;
boolean found = false;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (arr[i][j] == key) {
System.out.println("Element " + key + " found at (" + i + "," + j + ")");
found = true;
break;
}
}
if (found) break;
}
if (!found)
System.out.println("Element " + key + " not found!");
}
}
Sum of all elements in 2D Array (9*9)
public class Sum9x9 {
public static void main(String[] args) {
int[][] arr = new int[9][9];
int val = 1;
// Fill 1 to 81
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
arr[i][j] = val++;
}
long sum = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sum += arr[i][j];
System.out.println("Sum of all elements = " + sum);
Su-Do-Ku Puzzle
// Sudoku Solver in Java using Backtracking
public class SudokuSolver9x9 {
// Function to check if a number can be placed in a row
private boolean isValidRow(int[][] board, int r, int num) {
for (int i = 0; i < 9; i++) {
if (board[r][i] == num) // if number already exists in row
return false;
return true;
// Function to check if a number can be placed in a column
private boolean isValidCol(int[][] board, int c, int num) {
for (int i = 0; i < 9; i++) {
if (board[i][c] == num) // if number already exists in column
return false;
return true;
}
// Function to check if a number can be placed inside a 3x3 sub-grid
private boolean isValidBox(int[][] board, int r, int c, int num) {
// Find top-left corner of the 3x3 box
int startRow = (r / 3) * 3;
int startCol = (c / 3) * 3;
// Check inside this 3x3 box
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[startRow + i][startCol + j] == num)
return false;
return true;
// Combined function to check if placing num is valid
private boolean isValid(int[][] board, int r, int c, int num) {
return isValidRow(board, r, num) &&
isValidCol(board, c, num) &&
isValidBox(board, r, c, num);
// Backtracking function to solve the Sudoku
public boolean solve(int[][] board) {
for (int i = 0; i < 9; i++) { // loop through rows
for (int j = 0; j < 9; j++) { // loop through columns
if (board[i][j] == 0) { // empty cell found
for (int num = 1; num <= 9; num++) {
if (isValid(board, i, j, num)) {
board[i][j] = num; // place number
// recursively solve for next cells
if (solve(board))
return true;
// if failed, backtrack (reset cell)
board[i][j] = 0;
// no valid number found → backtrack
return false;
// if no empty cell is left, puzzle is solved
return true;
// Function to print the Sudoku board
public static void printBoard(int[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
System.out.println();
// Main method to test Sudoku solver
public static void main(String[] args) {
// 0 represents empty cells
int[][] board = {
{5,3,0,0,7,0,0,0,0},
{6,0,0,1,9,5,0,0,0},
{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},
{4,0,0,8,0,3,0,0,1},
{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},
{0,0,0,4,1,9,0,0,5},
{0,0,0,0,8,0,0,7,9}
};
System.out.println("Before Solving:");
printBoard(board);
SudokuSolver9x9 solver = new SudokuSolver9x9();
solver.solve(board);
System.out.println("\nAfter Solving:");
printBoard(board);