Binary Matrix after flipping submatrices in given range for Q queries | Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call
function manipulation ( matrix , q ) { let x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( let i = x1 - 1 ; i < x2 ; i ++ ) { for ( let j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i ] [ j ] == 1 ) matrix [ i ] [ j ] = 0 ; else matrix [ i ] [ j ] = 1 ; } } } function queries_fxn ( matrix , queries ) { for ( let q of queries ) manipulation ( matrix , q ) ; } let matrix = [ [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] ; let queries = [ [ 1 , 1 , 2 , 3 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 3 ] ] ; queries_fxn ( matrix , queries ) ; document . write ( " " ) ; for ( let i = 0 ; i < matrix . length ; i ++ ) { document . write ( " " ) ; for ( let j = 0 ; j < matrix [ i ] . length ; j ++ ) { if ( j < matrix [ i ] . length - 1 ) { document . write ( matrix [ i ] [ j ] + " " ) ; } else { document . write ( matrix [ i ] [ j ] + " " ) ; } } if ( i == matrix . length - 1 ) document . write ( " " ) ; else document . write ( " " ) ; } document . write ( " " ) ;