Minimum cells to be flipped to get a 2 * 2 submatrix with equal elements | Function to return the minimum flips required such that the submatrix from mat [ i ] [ j ] to mat [ i + 1 ] [ j + 1 ] contains all equal elements ; Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements ; To store the result ; For every submatrix of size 2 * 2 ; Update the count of flips required for the current submatrix ; Driver code
function minFlipsSub ( mat , i , j ) { var cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] . charAt ( j ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] . charAt ( j + 1 ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j + 1 ) == ' ' ) cnt1 ++ ; else cnt0 ++ ; return Math . min ( cnt0 , cnt1 ) ; } function minFlips ( mat , r , c ) { var res = Number . MAX_VALUE ; for ( i = 0 ; i < r - 1 ; i ++ ) { for ( j = 0 ; j < c - 1 ; j ++ ) { res = Math . min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } var mat = [ " " , " " , " " ] ; var r = mat . length ; var c = mat [ 0 ] . length ; document . write ( minFlips ( mat , r , c ) ) ;