给定一个大小为N的数组arr[] ,任务是找到需要插入数组的最小元素数,使数组的总和等于数组 XOR 的两倍。
例子:
Input: arr[] = {1, 2, 3, 6}
Output: 0
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: 1
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.
方法:想法是计算以下步骤以找到答案:
- 最初,我们找到数组的总和和数组的异或。
- 现在,我们检查给定的条件是否满足。如果满足,则打印 0,因为我们不需要插入任何元素。
- 现在,检查 XOR 是否等于 0。如果是,那么需要插入到数组中的元素就是数组中所有元素的总和。
- 这是因为,通过插入总和,新的 XOR 变为 (0 ^ sum = sum) 并且数组的总和变为 sum + sum = 2 * sum。因此条件满足。
- 否则,我们再添加两个元素,它们是 XOR 和 (sum + XOR)。这是因为:
NewXor = (Xor ^ (sum + Xor) ^ Xor) = Sum + Xor.
NewSum = (Sum + (Sum + Xor) + Xor) = 2 * (Sum + Xor) = 2 * NewXor
下面是上述方法的实现:
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