Count of elements to be inserted to make Array sum twice the XOR of Array

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output: 

1
6





 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : chitranayal, rock_cool