XOR of a subarray ( range of elements ) | Set 2 | C ++ program to find XOR in a range from L to R ; Function to find XOR in a range from L to R ; Compute xor from arr [ 0 ] to arr [ i ] ; process every query in constant time ; if L == 0 ; Driver Code ; query [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_Xor ( int arr [ ] , pair < int , int > query [ ] , int N , int Q ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } int ans = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] . first == 0 ) ans = arr [ query [ i ] . second ] ; else ans = arr [ query [ i ] . first - 1 ] ^ arr [ query [ i ] . second ] ; cout << ans << endl ; } } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 } ; int N = 8 ; int Q = 2 ; pair < int , int > query [ Q ] = { { 1 , 4 } , { 3 , 7 } } ; find_Xor ( arr , query , N , Q ) ; return 0 ; }