Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | Function to check whether Matrix A [ ] [ ] can be transformed to Matrix B [ ] [ ] or not ; Traverse the matrix to perform horizontal operations ; Calculate difference ; Update next X elements ; Traverse the matrix to perform vertical operations ; Calculate difference ; Update next K elements ; A [ i ] [ j ] is not equal to B [ i ] [ j ] ; Conversion is not possible ; Conversion is possible ; Input
function Check ( A , B , M , N , X ) { for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j <= N - X ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { var diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( var k = 0 ; k < X ; k ++ ) { A [ i ] [ j + k ] = A [ i ] [ j + k ] + diff ; } } } } for ( var i = 0 ; i <= M - X ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { var diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( var k = 0 ; k < X ; k ++ ) { A [ i + k ] [ j ] = A [ i + k ] [ j ] + diff ; } } } } for ( var i = 0 ; i < M ; i ++ ) { for ( var j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { return 0 ; } } } return 1 ; } var M = 2 , N = 2 , X = 2 ; var A = [ [ 0 , 0 ] , [ 0 , 0 ] ] ; var B = [ [ 1 , 2 ] , [ 0 , 1 ] ] ; if ( Check ( A , B , M , N , X ) ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; }