Count subarrays having even Bitwise XOR | Java program for the above approach ; Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Driver Code ; Given array ; Stores the size of the array ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void evenXorSubarray ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int XOR = 0 ; for ( int j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; evenXorSubarray ( arr , N ) ; } }