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 ; Print the answer ; Given Input ; Function Call
def Or_of_Ands_for_each_query ( arr , n , queries , q ) : NEW_LINE INDENT bits = [ 0 for x in range ( 32 ) ] NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( 1 << i ) & arr [ j ] ) : NEW_LINE INDENT bits [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT for p in range ( 0 , q ) : NEW_LINE INDENT for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) : NEW_LINE INDENT bits [ i ] -= 1 NEW_LINE DEDENT if ( queries [ p ] [ 1 ] & ( 1 << i ) ) : NEW_LINE INDENT bits [ i ] += 1 NEW_LINE DEDENT DEDENT arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ] NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( bits [ i ] != 0 ) : NEW_LINE INDENT ans |= ( 1 << i ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE q = 2 NEW_LINE arr = [ 3 , 5 , 7 ] NEW_LINE queries = [ [ 1 , 2 ] , [ 2 , 1 ] ] NEW_LINE Or_of_Ands_for_each_query ( arr , n , queries , q ) NEW_LINE