Count of elements to be inserted to make Array sum twice the XOR of Array | Function to find the minimum number of elements that need to be inserted such that the sum of the elements of the array is twice the XOR of the array ; Variable to store the Xor of all the elements ; Variable to store the sum of all elements ; Loop to find the Xor and the sum of the array ; If sum = 2 * Xor ; No need to insert more elements ; We insert one more element which is Sum ; We insert two more elements Sum + Xor and Xor . ; Print the number of elements inserted in the array ; Print the elements that are inserted in the array ; Driver code
function insert_element ( a , n ) { let Xor = 0 ; let Sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { Xor ^= a [ i ] ; Sum += a [ i ] ; } if ( Sum == 2 * Xor ) { document . write ( " " + " " ) ; return ; } if ( Xor == 0 ) { document . write ( " " + " " ) ; document . write ( Sum + " " ) ; return ; } let num1 = Sum + Xor ; let num2 = Xor ; document . write ( " " + " " ) ; document . write ( num1 + " " + num2 + " " ) ; } let a = [ 1 , 2 , 3 ] ; let n = a . length ; insert_element ( a , n ) ;