Minimum insertions to make XOR of an Array equal to half of its sum | Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver Code
function make_xor_half ( arr ) { let sum = 0 , xr = 0 ; for ( let a = 0 ; a < arr . length ; a ++ ) { sum += arr [ a ] ; xr ^= arr [ a ] ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . push ( sum ) ; return 1 ; } arr . push ( xr ) ; arr . push ( sum + xr ) ; return 2 ; } let N = 7 ; let nums = [ 3 , 4 , 7 , 1 , 2 , 5 , 6 ] ; let count = make_xor_half ( nums ) ; if ( count == - 1 ) document . write ( " " ) ; else if ( count == 1 ) document . write ( nums [ N ] + " " ) ; else document . write ( nums [ N ] + " " + nums [ N + 1 ] ) ;