Find number of square of area Z which can be built in a matrix having blocked regions | Function to calculate the number of square areas of size K * K ; Row array and column array to store the lengths of differences between consecutive rows / columns ; Fill the conrow vector ; Fill the concol vector ; To store the required answer ; Every pair of row size and column size would result in an unblocked region ; Driver code
function subgrids ( N , Z , row , col , r , c ) { var conrow = [ ] ; var concol = [ ] ; var K = Math . sqrt ( Z ) ; conrow . push ( row [ 0 ] - 0 - 1 ) ; conrow . push ( N + 1 - row [ r - 1 ] - 1 ) ; for ( var i = 1 ; i < r ; i ++ ) { conrow . push ( row [ i ] - row [ i - 1 ] - 1 ) ; } concol . push ( col [ 0 ] - 0 - 1 ) ; concol . push ( N + 1 - col - 1 ) ; for ( var i = 1 ; i < c ; i ++ ) { concol . push ( col [ i ] - col [ i - 1 ] - 1 ) ; } var row_size = conrow . length ; var col_size = concol . length ; var answer = 0 ; for ( var i = 0 ; i < row_size ; i ++ ) { for ( var j = 0 ; j < col_size ; j ++ ) { var total = parseInt ( concol [ j ] / K ) * parseInt ( conrow [ i ] / K ) ; answer += ( total ) ; } } return answer ; } var N = 8 , Z = 4 ; var row = [ 4 , 6 ] ; var col = [ 3 , 8 ] ; var r = row . length ; var c = col . length ; document . write ( subgrids ( N , Z , row , col , r , c ) ) ;