XOR of a subarray ( range of elements ) | Set 2 | Java 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 [ ]
class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_Xor ( int arr [ ] , pair 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 ] ; System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 } ; int N = 8 ; int Q = 2 ; pair query [ ] = { new pair ( 1 , 4 ) , new pair ( 3 , 7 ) } ; find_Xor ( arr , query , N , Q ) ; } }