Count ways to split array into three non | Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Given Input ; Function Call
function countWays ( arr , N ) { let arr_xor = 0 ; for ( let i = 0 ; i < N ; i ++ ) arr_xor ^= arr [ i ] ; let pref_xor = 0 , suff_xor = 0 ; let pref_ind = [ ] ; let suff_inds = new Array ( N + 1 ) ; suff_inds . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { pref_xor ^= arr [ i ] ; if ( pref_xor == arr_xor ) pref_ind . push ( i ) ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { suff_xor ^= arr [ i ] ; suff_inds [ i ] += suff_inds [ i + 1 ] ; if ( suff_xor == arr_xor ) suff_inds [ i ] ++ ; } let tot_ways = 0 ; for ( let idx of pref_ind ) { if ( idx < N - 1 ) tot_ways += suff_inds [ idx + 2 ] ; } return tot_ways ; } let arr = [ 7 , 0 , 5 , 2 , 7 ] ; let N = arr . length ; document . write ( countWays ( arr , N ) ) ;