Construct the Array using given bitwise AND , OR and XOR | Python3 program for the above approach ; Function to find the array ; Loop through all bits in number ; If bit is set in AND then set it in every element of the array ; If bit is not set in AND ; But set in b ( OR ) ; Set bit position in first element ; If bit is not set in c then set it in second element to keep xor as zero for bit position ; Calculate AND , OR and XOR of array ; Check if values are equal or not ; If not , then array is not possible ; Driver Code ; Given Bitwise AND , OR , and XOR ; Function Call
import sys NEW_LINE def findArray ( n , a , b , c ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) NEW_LINE for bit in range ( 30 , - 1 , - 1 ) : NEW_LINE INDENT set = a & ( 1 << bit ) NEW_LINE if ( set ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] |= set NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( b & ( 1 << bit ) ) : NEW_LINE INDENT arr [ 0 ] |= ( 1 << bit ) NEW_LINE if ( not ( c & ( 1 << bit ) ) ) : NEW_LINE INDENT arr [ 1 ] |= ( 1 << bit ) NEW_LINE DEDENT DEDENT DEDENT DEDENT aa = sys . maxsize NEW_LINE bb = 0 NEW_LINE cc = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT aa &= arr [ i ] NEW_LINE bb |= arr [ i ] NEW_LINE cc ^= arr [ i ] NEW_LINE DEDENT if ( a == aa and b == bb and c == cc ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE a = 4 NEW_LINE b = 6 NEW_LINE c = 6 NEW_LINE findArray ( n , a , b , c ) NEW_LINE DEDENT