Skip to content
Related Articles

Related Articles

Count of elements to be inserted to make Array sum twice the XOR of Array
  • Difficulty Level : Hard
  • Last Updated : 26 Mar, 2021

Given an array arr[] of size N, the task is to find the minimum number of elements that need to be inserted into the array so that the sum of the array is equal to two times the XOR of the array
Examples: 
 

Input: arr[] = {1, 2, 3, 6} 
Output:
Explanation: 
Xor = (1 ^ 2 ^ 3 ^ 6) = 6 and sum of the array = 12. 
The required condition (sum = 2 * Xor) satisfies. So no need to insert more elements.
Input: arr[] = {1, 2, 3} 
Output:
Explanation: 
Xor = (1 ^ 2 ^ 3) = 0 and sum of the array = (1 + 2 + 3) = 6. 
Here, we insert one element {6} into the array. Now, NewXor = (0 ^ 6) = 6 and NewSum = (6 + 6) = 12. 
Hence, NewSum = 2*NewXor. 
So, the given condition satisfies. 
 

 

Approach: The idea is to compute the following steps in order to find the answer: 
 

  • Initially, we find the sum of the array and the XOR of the array.
  • Now, we check if the given condition satisfies or not. If it satisfies, then print 0 as we need not insert any element.
  • Now, check if the XOR is equal to 0 or not. If it is, then the element that needs to be inserted into the array is the sum of all the elements of the array.
  • This is because, by inserting the sum, the new XOR becomes (0 ^ sum = sum) and the sum of the array becomes sum + sum = 2 * sum. Therefore the condition satisfies.
  • Else, we add two more elements which are XOR and (sum + XOR). This is because: 
     

NewXor = (Xor ^ (sum + Xor) ^ Xor) = Sum + Xor. 
NewSum = (Sum + (Sum + Xor) + Xor) = 2 * (Sum + Xor) = 2 * NewXor 
 



Below is the implementation of the above approach:
 

C++




// C++ program to find the count
// of elements to be inserted to
// make Array sum twice the XOR of Array
#include<bits/stdc++.h>
using namespace std;
 
// 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
void insert_element(int a[], int n)
{
     
    // Variable to store the
    // Xor of all the elements
    int Xor = 0;
 
    // Variable to store the
    // sum of all elements
    int Sum = 0;
     
    // Loop to find the Xor
    // and the sum of the array
    for(int i = 0; i < n; i++)
    
        Xor ^= a[i];
        Sum += a[i];
    }
     
    // If sum = 2 * Xor
    if(Sum == 2 * Xor)
    {
       
        // No need to insert
        // more elements
        cout << "0" << endl;
        return;
     }
   
    // We insert one more element
    // which is Sum
    if(Xor == 0)
    {
        cout << "1" << endl;
        cout << Sum << endl;
        return;
     }
 
    // We insert two more elements
    // Sum + Xor and Xor.
    int num1 = Sum + Xor;
     
    int num2 = Xor;
 
    // Print the number of elements
    // inserted in the array
    cout << "2";
 
    // Print the elements that are
    // inserted in the array
    cout << num1 << " "
         << num2 << endl; 
}
 
// Driver code
int main()
{   
    int a[] = {1, 2, 3};
    int n = sizeof(a) / sizeof(a[0]);
   
    insert_element(a, n);
}
 
// This code is contributed by chitranayal

Java




// Java program to find the count
// of elements to be inserted to
// make Array sum twice the XOR of Array
class GFG{
  
// 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
static void insert_element(int a[], int n)
{
      
    // Variable to store the
    // Xor of all the elements
    int Xor = 0;
  
    // Variable to store the
    // sum of all elements
    int Sum = 0;
      
    // Loop to find the Xor
    // and the sum of the array
    for(int i = 0; i < n; i++)
    
        Xor ^= a[i];
        Sum += a[i];
    }
      
    // If sum = 2 * Xor
    if(Sum == 2 * Xor)
    {
        
        // No need to insert
        // more elements
        System.out.println("0");
        return;
     }
    
    // We insert one more element
    // which is Sum
    if(Xor == 0)
    {
        System.out.println("1");
        System.out.println(Sum);
        return;
     }
  
    // We insert two more elements
    // Sum + Xor and Xor.
    int num1 = Sum + Xor;
      
    int num2 = Xor;
  
    // Print the number of elements
    // inserted in the array
    System.out.print("2");
  
    // Print the elements that are
    // inserted in the array
    System.out.println(num1 + " " + num2);
}
  
// Driver code
public static void main(String[] args)
{   
    int a[] = {1, 2, 3};
    int n = a.length;
    
    insert_element(a, n);
}
}
 
// This code is contributed by rock_cool

Python3




# Python program to find the 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
def insert_element(a, n):
     
    # Variable to store the
    # Xor of all the elements
    Xor = 0
 
    # Variable to store the
    # sum of all elements
    Sum = 0
     
    # Loop to find the Xor
    # and the sum of the array
    for i in range(n):
         
        Xor^= a[i]
        Sum+= a[i]
     
    # If sum = 2 * Xor
    if(Sum == 2 * Xor):
 
        # No need to insert
        # more elements
        print(0)
        return
 
    # We insert one more element
    # which is Sum
    if(Xor == 0):
        print(1)
        print(Sum)
        return
 
    # We insert two more elements
    # Sum + Xor and Xor.
    num1 = Sum + Xor
     
    num2 = Xor
 
    # Print the number of elements
    # inserted in the array
    print(2)
 
    # Print the elements that are
    # inserted in the array
    print(num1, num2)
     
         
# Driver code
if __name__ == "__main__":
     
    a = [1, 2, 3]
    n = len(a)
    insert_element(a, n)

C#




// C# program to find the count
// of elements to be inserted to
// make Array sum twice the XOR of Array
using System;
class GFG{
   
// 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
static void insert_element(int[] a, int n)
{
       
    // Variable to store the
    // Xor of all the elements
    int Xor = 0;
   
    // Variable to store the
    // sum of all elements
    int Sum = 0;
       
    // Loop to find the Xor
    // and the sum of the array
    for(int i = 0; i < n; i++)
    
        Xor ^= a[i];
        Sum += a[i];
    }
       
    // If sum = 2 * Xor
    if(Sum == 2 * Xor)
    {
         
        // No need to insert
        // more elements
        Console.Write("0");
        return;
     }
     
    // We insert one more element
    // which is Sum
    if(Xor == 0)
    {
        Console.Write("1" + '\n');
        Console.Write(Sum);
        return;
     }
   
    // We insert two more elements
    // Sum + Xor and Xor.
    int num1 = Sum + Xor;
       
    int num2 = Xor;
   
    // Print the number of elements
    // inserted in the array
    Console.Write("2");
   
    // Print the elements that are
    // inserted in the array
    Console.Write(num1 + " " + num2);
}
   
// Driver code
public static void Main(string[] args)
{   
    int[] a = {1, 2, 3};
    int n = a.Length;
     
    insert_element(a, n);
}
}
  
// This code is contributed by Ritik Bansal

Javascript




<script>
 
    // Javascript program to find the 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
    function insert_element(a, n)
    {
 
        // Variable to store the
        // Xor of all the elements
        let Xor = 0;
 
        // Variable to store the
        // sum of all elements
        let Sum = 0;
 
        // Loop to find the Xor
        // and the sum of the array
        for(let i = 0; i < n; i++)
        {
            Xor ^= a[i];
            Sum += a[i];
        }
 
        // If sum = 2 * Xor
        if(Sum == 2 * Xor)
        {
 
            // No need to insert
            // more elements
            document.write("0" + "</br>");
            return;
         }
 
        // We insert one more element
        // which is Sum
        if(Xor == 0)
        {
            document.write("1" + "</br>");
            document.write(Sum + "</br>");
            return;
         }
 
        // We insert two more elements
        // Sum + Xor and Xor.
        let num1 = Sum + Xor;
 
        let num2 = Xor;
 
        // Print the number of elements
        // inserted in the array
        document.write("2" + "</br>");
 
        // Print the elements that are
        // inserted in the array
        document.write(num1 + " " + num2 + "</br>");
    }
 
    let a = [1, 2, 3];
    let n = a.length;
    
    insert_element(a, n);
     
</script>
Output: 
1
6

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :