Queries to find the XOR of an Array after replacing all occurrences of X by Y | C ++ Program to implement the above approach ; Stores the bitwise XOR of array elements ; Function to find the total xor ; Loop to find the xor of all the elements ; Function to find the XOR after replacing all occurrences of X by Y for Q queries ; Remove contribution of X from total_xor ; Adding contribution of Y to total_xor ; Print total_xor ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int total_xor ; void initialize_xor ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { total_xor = total_xor ^ arr [ i ] ; } } void find_xor ( int X , int Y ) { total_xor = total_xor ^ X ; total_xor = total_xor ^ Y ; cout << total_xor << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 5 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; initialize_xor ( arr , n ) ; vector < vector < int > > Q = { { 5 , 6 } , { 8 , 1 } } ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { find_xor ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; } return 0 ; }