Make Array elements equal by replacing adjacent elements with their XOR | Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver Code ; Function Call
function possibleEqualArray ( A , N ) { let pref = new Array ( N ) ; pref [ 0 ] = A [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { document . write ( " " ) ; return ; } let cur_xor = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( let j = 0 ; j < i ; j ++ ) { if ( j ) { let middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; let left_xor = pref [ j - 1 ] ; let right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { document . write ( " " ) ; return ; } } } } document . write ( " " ) ; } let A = [ 0 , 2 , 2 ] ; let N = A . length ; possibleEqualArray ( A , N ) ;