Count zeros in a row wise and column wise sorted matrix | PHP program to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; Function to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; start from bottom - left corner of the matrix ; stores number of zeroes in the matrix ; move up until you find a 0 ; if zero is not found in current column , we are done ; add 0 s present in current column to result ; move right to next column ; Driver Code
< ? php $ N = 5 ; function countZeroes ( $ mat ) { $ row = $ N - 1 ; $ col = 0 ; $ count = 0 ; while ( $ col < $ N ) { while ( $ mat [ $ row ] [ $ col ] ) if ( -- $ row < 0 ) return $ count ; $ count += ( $ row + 1 ) ; $ col ++ ; } return $ count ; } $ mat = array ( array ( 0 , 0 , 0 , 0 , 1 ) , array ( 0 , 0 , 0 , 1 , 1 ) , array ( 0 , 1 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 , 1 ) ) ; echo countZeroes ( $ mat ) ; ? >