text: Find N distinct numbers whose Bitwise XOR is equal to K | C program for the above approach ; 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 ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call
code: #include <stdio.h> NEW_LINE void findArray ( int N , int K ) { if ( N == 1 ) { printf ( " % d " , K ) ; return ; } if ( N == 2 ) { printf ( " % d ▁ % d " , 0 , K ) ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { printf ( " % d ▁ " , i ) ; VAL ^= i ; } if ( VAL == K ) { printf ( " % d ▁ % d ▁ % d " , P , Q , P ^ Q ) ; } else { printf ( " % d ▁ % d ▁ % d " , 0 , P , P ^ K ^ VAL ) ; } } int main ( ) { int N = 4 , X = 6 ; findArray ( N , X ) ; return 0 ; }