给定一个正整数数组,任务是找到要在该数组中进行插入的最小次数,以使该数组的XOR等于其总和的一半,即2 *所有元素的Xor =所有元素的总和
例子:
Input: arr[] = {1 2 3 4 5}
Output: 1 16
Explanation:
In the modified array {1 2 3 4 5 1 16},
Sum = 1 + 2 + 3 + 4 + 5 + 1 + 16 = 32
Xor = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 1 ^ 16 = 16
And, 2 * 16 == 32
Thus, the condition 2 * Xor of all elements = Sum of all elements is satisfied.
Input: 7 11 3 25 51 32 9 29
Output: 17 184
Explanation:
In the modified array { 7 11 3 25 51 32 9 29 17 184}
Sum = 7 + 11 + 3 + 25 + 51 + 32 + 9 + 29 + 17 + 184 = 368
Xor = 7 ^ 11 ^ 3 ^ 25 ^ 51 ^ 32 ^ 9 ^ 29 ^ 17 ^ 184 = 184
And, 2 * 184 == 368
Thus, the condition 2 * Xor of all elements = Sum of all elements is satisfied.
方法:
为了解决这个问题,我们需要关注XOR的两个基本属性:
- A xor A = 0
- 异或0 = A
我们需要按照以下步骤解决问题:
- 计算所有数组元素的总和(S)和所有元素的异或(X)。如果S == 2 * X ,则无需更改数组。在这种情况下,打印-1。
- 否则,请执行以下操作:
- 如果X = 0 ,只需将S插入数组。现在,XOR为S ,总和为2S 。
- 否则,将X添加到数组以使数组的新Xor等于0。然后,在数组中插入S + X。现在,总和为2(S + X) ,异或为S + X
下面是上述方法的实现。
C++
// C++ Program to make XOR of
// of all array elements equal
// to half of its sum
// by minimum insertions
#include
using namespace std;
// Function to make XOR of the
// array equal to half of its sum
int make_xor_half(vector& arr)
{
int sum = 0, xr = 0;
// Calculate the sum and
// Xor of all the elements
for (int a : arr) {
sum += a;
xr ^= a;
}
// If the required condition
// satisfies already, return
// the original array
if (2 * xr == sum)
return -1;
// If Xor is already zero,
// Insert sum
if (xr == 0) {
arr.push_back(sum);
return 1;
}
// Otherwise, insert xr
// and insert sum + xr
arr.push_back(xr);
arr.push_back(sum + xr);
return 2;
}
// Driver Code
int main()
{
int N = 7;
vector nums
= { 3, 4, 7, 1, 2, 5, 6 };
int count = make_xor_half(nums);
if (count == -1)
cout << "-1" << endl;
else if (count == 1)
cout << nums[N] << endl;
else
cout << nums[N] << " "
<< nums[N + 1] << endl;
return 0;
}
Python3
# Python3 program to make XOR of
# of all array elements equal to
# half of its sum by minimum
# insertions
# Function to make XOR of the
# array equal to half of its sum
def make_xor_half(arr):
sum = 0; xr = 0;
# Calculate the sum and
# Xor of all the elements
for a in arr:
sum += a;
xr ^= a;
# If the required condition
# satisfies already, return
# the original array
if (2 * xr == sum):
return -1;
# If Xor is already zero,
# Insert sum
if (xr == 0):
arr.append(sum);
return 1;
# Otherwise, insert xr
# and insert sum + xr
arr.append(xr);
arr.append(sum + xr);
return 2;
# Driver code
if __name__ == "__main__":
N = 7;
nums = [ 3, 4, 7, 1, 2, 5, 6 ];
count = make_xor_half(nums);
if (count == -1):
print("-1");
elif (count == 1):
print(nums[N]);
else:
print(nums[N], nums[N + 1]);
# This code is contributed by AnkitRai01
Java
// Java program to make XOR of all
// array elements equal to half
// of its sum by minimum insertions
import java.util.*;
class GFG{
// Function to make XOR of the
// array equal to half of its sum
static int make_xor_half(ArrayList arr)
{
int sum = 0, xr = 0;
// Calculate the sum and
// Xor of all the elements
for (int i = 0;
i < arr.size(); i++)
{
int a = arr.get(i);
sum += a;
xr ^= a;
}
// If the required condition
// satisfies already, return
// the original array
if (2 * xr == sum)
return -1;
// If Xor is already zero,
// Insert sum
if (xr == 0)
{
arr.add(sum);
return 1;
}
// Otherwise, insert xr
// and insert sum + xr
arr.add(xr);
arr.add(sum + xr);
return 2;
}
// Driver code
public static void main(String[] args)
{
int N = 7;
ArrayList nums =
new ArrayList(
Arrays.asList(3, 4, 7,
1, 2, 5, 6));
int count = make_xor_half(nums);
if (count == -1)
System.out.print(-1 + "\n");
else if (count == 1)
System.out.print(nums.get(N) + "\n");
else
System.out.print(nums.get(N) + " " +
nums.get(N + 1) + "\n");
}
}
// This code is contributed by Chitranayal
C#
// C# program to make XOR of all
// array elements equal to half
// of its sum by minimum insertions
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
// Function to make XOR of the
// array equal to half of its sum
static int make_xor_half(ArrayList arr)
{
int sum = 0, xr = 0;
// Calculate the sum and
// Xor of all the elements
foreach(int a in arr)
{
sum += a;
xr ^= a;
}
// If the required condition
// satisfies already, return
// the original array
if (2 * xr == sum)
return -1;
// If Xor is already zero,
// Insert sum
if (xr == 0)
{
arr.Add(sum);
return 1;
}
// Otherwise, insert xr
// and insert sum + xr
arr.Add(xr);
arr.Add(sum + xr);
return 2;
}
// Driver code
public static void Main(string[] args)
{
int N = 7;
ArrayList nums = new ArrayList(){ 3, 4, 7, 1,
2, 5, 6 };
int count = make_xor_half(nums);
if (count == -1)
Console.Write(-1 + "\n");
else if (count == 1)
Console.Write(nums[N] + "\n");
else
Console.Write(nums[N] + " " +
nums[N + 1] + "\n");
}
}
// This code is contributed by rutvik_56
Javascript
28
时间复杂度: O(N) ,其中N是数组的大小。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。