Count of ways to select K consecutive empty cells from a given Matrix | Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
function rowWise ( v , n , m , k ) { var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var countcons = 0 ; for ( var j = 0 ; j < m ; j ++ ) { if ( ( v + i * m + j ) == ' ' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } function colWise ( v , n , m , k ) { var ans = 0 ; for ( var i = 0 ; i < m ; i ++ ) { var countcons = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( ( v + j * n + i ) == ' ' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } var n = 3 , m = 3 , k = 1 ; var v = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; if ( k == 1 ) { document . write ( rowWise ( v [ 0 ] , n , m , k ) ) ; } else { document . write ( colWise ( v [ 0 ] , n , m , k ) + rowWise ( v [ 0 ] , n , m , k ) ) ; }