Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | Java program for the above approach ; 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 ; Driver Code ; Input
import java . io . * ; import java . util . * ; class GFG { static int Check ( int A [ ] [ ] , int B [ ] [ ] , int M , int N , int X ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j <= N - X ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { int diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i ] [ j + k ] = A [ i ] [ j + k ] + diff ; } } } } for ( int i = 0 ; i <= M - X ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { int diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i + k ] [ j ] = A [ i + k ] [ j ] + diff ; } } } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { return 0 ; } } } return 1 ; } public static void main ( String [ ] args ) { int M = 2 , N = 2 , X = 2 ; int A [ ] [ ] = { { 0 , 0 } , { 0 , 0 } } ; int B [ ] [ ] = { { 1 , 2 } , { 0 , 1 } } ; if ( Check ( A , B , M , N , X ) != 0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }