Construct the Array using given bitwise AND , OR and XOR | Java 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 java . io . * ; class GFG { static void findArray ( int n , int a , int b , int c ) { int arr [ ] = new int [ n + 1 ] ; for ( int bit = 30 ; bit >= 0 ; bit -- ) { int set = a & ( 1 << bit ) ; if ( set != 0 ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( ( b & ( 1 << bit ) ) != 0 ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ( c & ( 1 << bit ) ) == 0 ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } int aa = Integer . MAX_VALUE , bb = 0 , cc = 0 ; for ( int i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ; } }