XOR of every element of an Array with a given number K | C ++ program to find XOR of every element of an array with a given number K ; Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructXORArray ( int A [ ] , int n , int K ) { int B [ n ] ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( int i = 0 ; i < n ; i ++ ) cout << B [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int K = 5 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructXORArray ( A , n , K ) ; int B [ ] = { 4 , 75 , 45 , 42 } ; K = 2 ; n = sizeof ( B ) / sizeof ( B [ 0 ] ) ; constructXORArray ( B , n , K ) ; return 0 ; }