Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Prlet the answer ; Given Input ; Function Call
function Or_of_Ands_for_each_query ( arr , n , queries , q ) { let bits = new Array ( 32 ) . fill ( 0 ) ; for ( let i = 0 ; i < 32 ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( 1 << i ) & arr [ j ] ) { bits [ i ] ++ ; } } } for ( let p = 0 ; p < q ; p ++ ) { for ( let i = 0 ; i < 32 ; i ++ ) { if ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) { bits [ i ] -- ; } if ( queries [ p ] [ 1 ] & ( 1 << i ) ) { bits [ i ] ++ ; } } arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ] ; let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { if ( bits [ i ] != 0 ) { ans |= ( 1 << i ) ; } } document . write ( ans + " " ) ; } } let n = 3 , q = 2 ; let arr = [ 3 , 5 , 7 ] ; let queries = [ [ 1 , 2 ] , [ 2 , 1 ] ] ; Or_of_Ands_for_each_query ( arr , n , queries , q ) ;