Replace specified matrix elements such that no two adjacent elements are equal | Function to display the valid matrix ; Traverse the matrix ; If the current cell is a free space and is even - indexed ; If the current cell is a free space and is odd - indexed ; Print the matrix ; Given N and M ; Given matrix ; Function call
function print ( arr , n , m ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' ' ) { arr [ i ] [ j ] = ' ' ; } else if ( a == ' ' ) { arr [ i ] [ j ] = ' ' ; } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let n = 4 , m = 4 ; let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; print ( arr , n , m ) ;