📌  相关文章
📜  要插入的元素数量,以使Array的总和为Array的XOR的两倍

📅  最后修改于: 2021-05-14 08:01:12             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到需要插入到数组中的最小元素数,以使数组的总和等于数组XOR的两倍。
例子:

方法:想法是计算以下步骤以找到答案:

  • 最初,我们找到数组的总和与数组的XOR。
  • 现在,我们检查给定条件是否满足。如果满足,则打印0,因为我们不需要插入任何元素。
  • 现在,检查XOR是否等于0。如果是,则需要插入到数组中的元素是数组中所有元素的总和。
  • 这是因为,通过插入总和,新的XOR变为(0 ^ sum = sum),数组的总和变为sum + sum = 2 * sum。因此条件满足。
  • 否则,我们添加了两个元素XOR和(sum + XOR)。这是因为:

下面是上述方法的实现:

C++
// C++ program to find the count
// of elements to be inserted to
// make Array sum twice the XOR of Array
#include
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


输出:
1
6