Tuesday, September 26, 2017

ZOHO QUESTION : Given a 9×9 sudoku we have to evaluate it for its correctness. We have to check both the sub matrix correctness and the whole sudoku correctness.

Discussion and Analysis:

Here is some interesting question we have about checking sudoku correctness.

Here the rules of sudoku following,

For example we have 9*9 sudoku,

So we have 81 number of square grid. This grid divided into 9 blocks.Each block have 9 square so totally 81 square.

So it has 9 rows and 9 columns. Each rows have number 1-9. But a number appears only once in the row as well as same rule for column.


Solution : 


/**
     * Find correctness.
     *
     * @return true, if successful
     */
    private static boolean evaluateSudoku() {
//Here is our input as 9*9 matrix.
        int[][] l_arrSudukoMatrix = new int[][] {
                { 5, 1, 3, 6, 8, 7, 2, 4, 9 },
                { 8, 4, 9, 5, 2, 1, 6, 3, 7 },
                { 2, 6, 7, 3, 4, 9, 5, 8, 1 },
                { 1, 5, 8, 4, 6, 3, 9, 7, 2 },
                { 9, 7, 4, 2, 1, 8, 3, 6, 5 },
                { 3, 2, 6, 7, 9, 5, 4, 1, 8 },
                { 7, 8, 2, 9, 3, 4, 1, 5, 6 },
                { 6, 3, 5, 1, 7, 2, 8, 9, 4 },
                { 4, 9, 1, 8, 5, 6, 7, 2, 3 } };
        Set<Integer> l_stRowSet = new HashSet<Integer>();
        Set<Integer> l_stColumnSet = new HashSet<Integer>();
//Here haveDuplicate variable to return the true/false. If haveDuplicate is false the sudoku is valid one.Otherwise its wrong sudoku.
        boolean haveDuplicate = true;

        for (int i = 0; i < l_arrSudukoMatrix.length; i++) {
            for (int j = 0; j < l_arrSudukoMatrix[i].length; j++) {
//Here to check the entered value is greater than zero and less than 10
              if(l_arrSudukoMatrix[i][j] > 0 && l_arrSudukoMatrix[i][j] < 10){
                l_stRowSet.add(l_arrSudukoMatrix[i][j]);
                }
            }
            for (int j = 0; j < l_arrSudukoMatrix[i].length; j++) {
//Here to check the entered value is greater than zero and less than 10
              if(l_arrSudukoMatrix[j][i] > 0 && l_arrSudukoMatrix[j][i] < 10){
                l_stColumnSet.add(l_arrSudukoMatrix[j][i]);
                }
            }
//If Row/Column have less than 9 size we have some repeated value, so we can return true.
            if (l_stRowSet.size() != 9 && l_stColumnSet.size() != 9) {
                return false;
            }
        }

        return haveDuplicate;

    }

Output : For this given input matrix true will be return.
If you modify some input with repeated value or greater than 9 and less than zero then system return false.


NOTE: Please share your comments to improve the flag. Thank you!

7 comments:

  1. Instead of checking duplicates you can check the sum of numbers in each row or col or box shoul be equal to 45 .. is that right

    ReplyDelete
    Replies
    1. Yes, we can check. We will post the program with checking sum of the each row and column.

      Thank you!.

      Delete
    2. Hello, Here we have solution as u suggest https://boomirajp.blogspot.com/2022/05/zoho-question-given-99-sudoku-we-have.html

      Delete
  2. package SudokuCheck;

    import java.util.*;

    public class SudokuValidityCheck {
    public static boolean SolveSudoku() {
    int arr[][] = new int[][] { { 5, 1, 3, 6, 8, 7, 2, 4, 9 }, { 5, 4, 9, 5, 2, 1, 6, 3, 7 },
    { 2, 6, 7, 3, 4, 9, 5, 8, 1 }, { 1, 5, 8, 4, 6, 3, 9, 7, 2 }, { 9, 7, 4, 2, 1, 8, 3, 6, 5 },
    { 3, 2, 6, 7, 9, 5, 4, 1, 8 }, { 7, 8, 2, 9, 3, 4, 1, 5, 6 }, { 6, 3, 5, 1, 7, 2, 8, 9, 4 },
    { 4, 9, 1, 8, 5, 6, 7, 2, 3 } };
    Set rowSet = new HashSet();
    Set columnSet = new HashSet();
    for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
    if (arr[i][j] > 0 && arr[i][j] < 10) {
    rowSet.add(arr[i][j]);
    }
    }
    if (rowSet.size() != 9) {
    return false;
    }
    rowSet.clear();
    }
    for (int j = 0; j < arr.length; j++) {
    for (int i = 0; i < arr[j].length; i++) {
    if (arr[i][j] > 0 && arr[i][j] < 10) {
    columnSet.add(arr[i][j]);
    }
    }
    if (columnSet.size() != 9) {
    return false;
    }
    columnSet.clear();

    }

    return true;

    }

    public static void main(String args[]) {
    if (SolveSudoku()) {
    System.out.println("isValid");
    }
    else {
    System.out.println("isinValid");
    }

    }

    }

    It is the correct program for duplicate values,we should clear the set or else the set will have the number from 1 to 9 every time..This program doesn't validate the submatrix part,but it covers the row and column checking,if any body know the submatrix part inform me

    ReplyDelete
    Replies
    1. We no need to check submatrix validation, we all good with checking all row and column. Here i have simple solution

      Delete
  3. import java.util.*;
    class sudoko{
    public static void main(String args[]){

    int[][] board = {
    { 5, 1, 3, 6, 8, 7, 2, 4, 9 },
    { 8, 4, 9, 5, 2, 1, 6, 3, 7 },
    { 2, 6, 7, 3, 4, 9, 5, 8, 1 },
    { 1, 5, 8, 4, 6, 3, 9, 7, 2 },
    { 9, 7, 4, 2, 1, 8, 3, 6, 5 },
    { 3, 2, 6, 7, 9, 5, 4, 1, 8 },
    { 7, 8, 2, 9, 3, 4, 1, 5, 6 },
    { 6, 3, 5, 1, 7, 2, 8, 9, 4 },
    { 4, 9, 1, 8, 5, 6, 7, 2, 3 } };

    Set set=new HashSet<>();
    int flag=0;
    for(int i=0;i<board.length;i++){
    for(int j=0;j<board[0].length;j++){
    int ch=board[i][j];
    if(!(set.add(ch+" is at "+i+" th row")) || !(set.add(ch+" is at "+j+" th column")) || !(set.add(ch+" is at "+i/3+" th row "+j/3+" th column"))){
    flag=1;
    break;
    }


    }
    if(flag==1){
    break;
    }
    }
    if(flag==0){
    System.out.println("true");
    }
    else{
    System.out.println("false");
    }

    System.out.println(set);

    }


    }

    ReplyDelete
    Replies
    1. Your answer is unique and impressed. Here i have simple solution, https://boomirajp.blogspot.com/2022/05/zoho-question-given-99-sudoku-we-have.html

      Delete