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

📅  最后修改于: 2021-09-05 12:00:45             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到需要插入数组的最小元素数,使数组的总和等于数组 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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live