Why only one test case is not getting...?

import java.util.*;
public class Main {
public static int size;
public static int[][] board;

public static boolean isInRow(int row, int num){
	for(int i = 0; i < size; i++)
		if(board[row][i] == num)
			return true;
	return false;
}

public static boolean isInCol(int col, int num){
	for(int i = 0; i < size; i++)
		if(board[i][col] == num)
			return true;

	return false;
}

public static boolean isInBlock(int row, int col, int num){
	int r = row - row % 3;
	int c = col - col % 3;
	
	for(int i = r; i < r + 3; i++)
		for(int j = c; j < c + 3; j++)
			if(board[r][j] == num)
				return true;

		return false;
	
}

public static boolean isOk(int row, int col, int num){
	return !isInRow(row, num) && !isInCol(col, num) && !isInBlock(row, col, num);
}

public static boolean solve(){
	for(int row = 0; row < size; row++){
		for(int col = 0; col < size; col++){
			if(board[row][col] == 0){
				for(int num = 1; num <= size; num++){
					if(isOk(row,col,num)){
						board[row][col] = num;
						if(solve()){
							return true;
						}
						else{
							board[row][col] = 0;
						}
					}
				}
				return false;
			}
		}
	}
	return true;
}
public static void display(){
	for(int i = 0; i < size; i++){
		for(int j = 0; j < size; j++){
			System.out.print(board[i][j] + " ");
		}
		System.out.println();
	}
		
}
public static void main(String[] argc){
	Scanner sc = new Scanner(System.in);
	size = sc.nextInt();
	board = new int[size][size];
	for(int i = 0; i < size; i++)
		for(int j = 0; j < size; j++)
			board[i][j] = sc.nextInt();
	if(solve()){
		display();
	}
}

}

@prathamesh.kalebere,

You are not checking if a number will repeat in a 3x3 block.

Suggested Approach:

We can solve Sudoku by one by one assigning numbers to empty cells. Before assigning a number, we check whether it is safe to assign. We basically check that the same number is not present in the current row, current column and current 3X3 subgrid. After checking for safety, we assign the number, and recursively check whether this assignment leads to a solution or not. If the assignment doesn’t lead to a solution, then we try next number for the current empty cell. And if none of the number (1 to 9) leads to a solution, we return false.

  • Find row, col of an unassigned cell
  • If there is none, return true
  • For digits from 1 to 9
    • a) If there is no conflict for digit at row, col assign digit to row, col and recursively try fill in rest of grid
    • b) If recursion successful, return true
    • c) Else, remove digit and try another
  • If all digits have been tried and nothing worked, return false