Queries on XOR of XORs of all subarrays | CPP Program to answer queries on XOR of XORs of all subarray ; Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  100 NEW_LINE using namespace std ; void ansQueries ( int prefeven [ ] , int prefodd [ ] , int l , int r ) { if ( ( r - l + 1 ) % 2 == 0 ) cout << "0" ; else { if ( l % 2 == 0 ) cout << ( prefeven [ r ] ^ prefeven [ l - 1 ] ) ; else cout << ( prefodd [ r ] ^ prefodd [ l - 1 ] ) ; } cout << endl ; } void wrapper ( int arr [ ] , int n , int l [ ] , int r [ ] , int q ) { int prefodd [ N ] = { 0 } , prefeven [ N ] = { 0 } ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i ) % 2 == 0 ) { prefeven [ i ] = arr [ i - 1 ] ^ prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ; } else { prefeven [ i ] = prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ^ arr [ i - 1 ] ; } } int i = 0 ; while ( i != q ) { query ( prefeven , prefodd , l [ i ] , r [ i ] ) ; i ++ ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int l [ ] = { 1 , 1 , 2 } ; int r [ ] = { 2 , 3 , 4 } ; int q = sizeof ( l ) / sizeof ( l [ 0 ] ) ; ansQueries ( arr , n , l , r , q ) ; return 0 ; }