Count subarrays having even Bitwise XOR | Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Given array ; Stores the size of the array
function evenXorSubarray ( arr , n ) { let ans = 0 ; let freq = [ 0 , 0 ] ; let XOR = 0 ; for ( let i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; evenXorSubarray ( arr , N ) ;