Minimize flips required to make all shortest paths from top | Java program for the above approach ; Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Driver Code ; Given Matrix ; Given path as a string ; Function Call
class GFG { static int minFlips ( int mat [ ] [ ] , String s ) { int N = mat . length ; int M = mat [ 0 ] . length ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s . charAt ( i + j ) - '0' ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; String s = "10001" ; System . out . print ( minFlips ( mat , s ) ) ; } }