Find N distinct numbers whose Bitwise XOR is equal to K | Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Prlet the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
function findArray ( N , K ) { if ( N == 1 ) { document . write ( K + " " ) ; return ; } if ( N == 2 ) { document . write ( 0 + " " + K ) ; return ; } let P = N - 2 ; let Q = N - 1 ; let VAL = 0 ; for ( let i = 1 ; i <= ( N - 3 ) ; i ++ ) { document . write ( i + " " ) ; VAL ^= i ; } if ( VAL == K ) { document . write ( P + " " + Q + " " + ( P ^ Q ) ) ; } else { document . write ( 0 + " " + P + " " + ( P ^ K ^ VAL ) ) ; } } let N = 4 , X = 6 ; findArray ( N , X ) ;