Home > Enterprise >  What is N-2 doing in the Sudoku for loop to validate the block JavaScript
What is N-2 doing in the Sudoku for loop to validate the block JavaScript

Time:01-22

I was researching solution to the Sudoku Solution Validator algorithm and came across this example. The code works and validates but what I do not understand is why in the for loops to get to validate a block there is a N-2? If N = 9 and the board is 9 * 9 then why would that need to be changed to 7?

When I remove the -2 and just leave N I do not see any changes in my console.

Here is the link https://www.geeksforgeeks.org/check-if-given-sudoku-solution-is-valid-or-not/

Thank you!!

<script>

// JavaScript program to implement
// the above approach

var N = 9;

// Function to check if all elements
// of the board[][] array store
// value in the range[1, 9]
function isinRange(board)
{
    
    // Traverse board[][] array
    for(var i = 0; i < N; i  )
    {
        for(var j = 0; j < N; j  )
        {
            
            // Check if board[i][j]
            // lies in the range
            if (board[i][j] <= 0 ||
                board[i][j] > 9)
            {
                return false;
            }
        }
    }
    return true;
}

// Function to check if the solution
// of sudoku puzzle is valid or not
function isValidSudoku(board)
{
    
    // Check if all elements of board[][]
    // stores value in the range[1, 9]
    if (isinRange(board) == false)
    {
        return false;
    }

    // Stores unique value
    // from 1 to N
    var unique = Array(N 1).fill(false);

    // Traverse each row of
    // the given array
    for(var i = 0; i < N; i  )
    {
        unique = Array(N 1).fill(false);

        // Traverse each column
        // of current row
        for(var j = 0; j < N; j  )
        {

            // Stores the value
            // of board[i][j]
            var Z = board[i][j];

            // Check if current row
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }

    // Traverse each column of
    // the given array
    for(var i = 0; i < N; i  )
    {

        // Initialize unique[]
        // array to false
        unique = Array(N 1).fill(false);

        // Traverse each row
        // of current column
        for(var j = 0; j < N; j  )
        {

            // Stores the value
            // of board[j][i]
            var Z = board[j][i];

            // Check if current column
            // stores duplicate value
            if (unique[Z])
            {
                return false;
            }
            unique[Z] = true;
        }
    }

    // Traverse each block of
    // size 3 * 3 in board[][] array
    for(var i = 0; i < N - 2; i  = 3) //<====== what is the point of N-2? What is it doing?
    {
        
        // j stores first column of
        // each 3 * 3 block
        for(var j = 0; j < N - 2; j  = 3) //<====== what is the point of N-2? What is it doing?
        {

            // Initialize unique[]
            // array to false
            unique = Array(N 1).fill(false);

            // Traverse current block
            for(var k = 0; k < 3; k  )
            {
                for(var l = 0; l < 3; l  )
                {
                    
                    // Stores row number
                    // of current block
                    var X = i   k;

                    // Stores column number
                    // of current block
                    var Y = j   l;

                    // Stores the value
                    // of board[X][Y]
                    var Z = board[X][Y];

                    // Check if current block
                    // stores duplicate value
                    if (unique[Z])
                    {
                        return false;
                    }
                    unique[Z] = true;
                }
            }
        }
    }

    // If all conditions satisfied
    return true;
}

// Driver Code
var board = [ [ 7, 9, 2, 1, 5, 4, 3, 8, 6 ],
                [ 6, 4, 3, 8, 2, 7, 1, 5, 9 ],
                [ 8, 5, 1, 3, 9, 6, 7, 2, 4 ],
                [ 2, 6, 5, 9, 7, 3, 8, 4, 1 ],
                [ 4, 8, 9, 5, 6, 1, 2, 7, 3 ],
                [ 3, 1, 7, 4, 8, 2, 9, 6, 5 ],
                [ 1, 3, 6, 7, 4, 8, 5, 9, 2 ],
                [ 9, 7, 4, 2, 1, 5, 6, 3, 8 ],
                [ 5, 2, 8, 6, 3, 9, 4, 1, 7 ] ];
if (isValidSudoku(board))
{
    document.write("Valid");
}
else
{
    document.write("Not Valid");
}


</script>

CodePudding user response:

enter image description here


Sudoku contains sub blocks each is 3X3
So the code loops over the first cell in each sub block then iterate over the each sub-block cell.
The author of the code added N-2 condition so when he iterate over the sub-block cells Var X = i k; he make sure he doesn't access out of bound cell.
How ever when the number of columns and number of rows in sudoku are multiple of 3 this check is useless.
That's why you see no difference when you remove -2.

CodePudding user response:

This is because the code is working in steps of 3

for(var i = 0; i < N - 2; i  = 3)

You can see i = 3

so i = 0, 3, 6

as N = 9 -> 9-2 = 7 -> 6 is bigger than 7

Later in teh code you can see loop k & l, this is taking care of the 3x3 matrix

  •  Tags:  
  • Related