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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArray ( int N , int K ) { if ( N == 1 ) { cout << " ▁ " << K ; return ; } if ( N == 2 ) { cout << 0 << " ▁ " << K ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { cout << " ▁ " << i ; VAL ^= i ; } if ( VAL == K ) { cout << P << " ▁ " << Q << " ▁ " << ( P ^ Q ) ; } else { cout << 0 << " ▁ " << P << " ▁ " << ( P ^ K ^ VAL ) ; } } int main ( ) { int N = 4 , X = 6 ; findArray ( N , X ) ; return 0 ; }