Find number of endless points | Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage or column after this point , increment result . ; Driver code
< ? php function countEndless ( $ input , $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ isEndless = 1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ input [ $ i ] [ $ j ] == 0 ) $ isEndless = 0 ; $ col [ $ i ] [ $ j ] = $ isEndless ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ isEndless = 1 ; for ( $ j = $ n - 1 ; $ j >= 0 ; $ j -- ) { if ( $ input [ $ i ] [ $ j ] == 0 ) $ isEndless = 0 ; $ row [ $ i ] [ $ j ] = $ isEndless ; } } $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 1 ; $ j < $ n ; $ j ++ ) if ( $ row [ $ i ] [ $ j ] && $ col [ $ i ] [ $ j ] ) $ ans ++ ; return $ ans ; } $ input = array ( array ( 1 , 0 , 1 , 1 ) , array ( 0 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 ) , array ( 0 , 1 , 1 , 0 ) ) ; $ n = 4 ; echo countEndless ( $ input , $ n ) ; ? >