XOR of a subarray ( range of elements ) | Set 2 | 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 [ ]
function find_Xor ( arr , query , N , Q ) { for ( var i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } var ans = 0 ; for ( var i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 0 ) ans = arr [ query [ i ] [ 1 ] ] ; else ans = arr [ query [ i ] [ 0 ] - 1 ] ^ arr [ query [ i ] [ 1 ] ] ; document . write ( ans + " " ) ; } } var arr = [ 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 ] ; var N = 8 ; var Q = 2 ; var query = [ [ 1 , 4 ] , [ 3 , 7 ] ] ; find_Xor ( arr , query , N , Q ) ;