Find Number of Even cells in a Zero Matrix after Q queries | Function to find the number of even cell in a 2D matrix ; Maintain two arrays , one for rows operation and one for column operation ; Increment operation on row [ i ] ; Increment operation on col [ i ] ; Count odd and even values in both arrays and multiply them ; Count of rows having even numbers ; Count of rows having odd numbers ; Count of columns having even numbers ; Count of columns having odd numbers ; Driver code
function findNumberOfEvenCells ( n , q , size ) { let row = new Array ( n ) ; row . fill ( 0 ) ; let col = new Array ( n ) ; col . fill ( 0 ) ; for ( let i = 0 ; i < size ; i ++ ) { let x = q [ i ] [ 0 ] ; let y = q [ i ] [ 1 ] ; row [ x - 1 ] ++ ; col [ y - 1 ] ++ ; } let r1 = 0 , r2 = 0 ; let c1 = 0 , c2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( row [ i ] % 2 == 0 ) { r1 ++ ; } if ( row [ i ] % 2 == 1 ) { r2 ++ ; } if ( col [ i ] % 2 == 0 ) { c1 ++ ; } if ( col [ i ] % 2 == 1 ) { c2 ++ ; } } let count = r1 * c1 + r2 * c2 ; return count ; } let n = 2 ; let q = [ [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 1 ] ] ; let size = q . length ; document . write ( findNumberOfEvenCells ( n , q , size ) ) ;