Construct the Array using given bitwise AND , OR and XOR | 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 ; Given Bitwise AND , OR , and XOR ; Function Call
function findArray ( n , a , b , c ) { let arr = new Array ( n + 1 ) ; for ( let bit = 30 ; bit >= 0 ; bit -- ) { let set = a & ( 1 << bit ) ; if ( set ) { for ( let i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( b & ( 1 << bit ) ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ! ( c & ( 1 << bit ) ) ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } let aa = Number . MAX_SAFE_INTEGER , bb = 0 , cc = 0 ; for ( let i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } else document . write ( " " ) ; } let n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ;