求一个数 X,使得给定数组的每个元素加上 X 后的 XOR 为 0
给定一个包含正整数的奇数长度 N数组arr[] 。任务是找到一个正整数X ,这样,将X添加到arr[]的所有元素,然后对所有元素进行XOR得到0 。如果不存在这样的X ,则返回-1 。
例子:
Input: arr[] = {2, 4, 5}
Output: 1
Explanation: Following are the operations performed in arr[] to get the desired result.
Adding 1 to each element in arr[] updates arr[] to arr[] = {3, 5, 6}
Now XOR of all the elements in arr[] is 3^5^6 = 0.
Therefore, 1 is the required answer.
Input: arr[] = {4, 5, 13}
Output: -1
Explanation: No such x exists for fulfilling the desired conditions.
方法:奇数个1的XOR = 1,而偶数个1 = 0。这个想法可以用来解决给定的问题。请按照以下步骤操作:
- 初始化变量X = 0 。
- 数组元素的二进制表示将用于遍历元素并确定X 。
- 从第 0 位开始遍历到第 63 位。
- 如果在任何位位置,数组元素的设置位 (1) 总数为奇数,则将 2 的幂与 X 相加。
- 如果在迭代完成后在任何位位置有奇数个 1,则不存在这样的 X。否则,打印 X 作为答案。
请参见下面的插图:
Illustration:
Case-1 (X possible): Take arr[] = { 2, 4, 5} 5th 4th 3rd 2nd 1st 0th arr[0] 0 0 0 0 1 0 arr[1] 0 0 0 1 0 0 arr[2] 0 0 0 1 0 1 X 0 0 0 0 0 0
- Initially, X = 0 0 0 0 0 0, at 0th position set(1) bits are odd, so in order to make the set bits even, flip the bits at 0th position. So, for flipping the bits just add (0 0 0 0 0 1) to all the elements of arr[] and in X.
- Now, the table will look like the following:
5th | 4th | 3rd | 2nd | 1st | 0th | |
---|---|---|---|---|---|---|
arr[0] | 0 | 0 | 0 | 0 | 1 | 1 |
arr[1] | 0 | 0 | 0 | 1 | 0 | 1 |
arr[2] | 0 | 0 | 0 | 1 | 1 | 0 |
XOR | 0 | 0 | 0 | 0 | 0 | 0 |
X | 0 | 0 | 0 | 0 | 0 | 1 |
- Now, the XOR of (arr[0]+x) ^ ( arr[1]+x) ^ arr[2]+x) = 0, result will be 0. So, print res = X.
在不可能的情况下采取以下 X
Case-2: Take example: arr[] = { 4, 5, 13 } 5th 4th 3rd 2nd 1st 0th arr[0] 0 0 0 1 0 0 arr[1] 0 0 0 1 0 1 arr[2] 0 0 1 1 0 1 XOR 0 0 1 1 0 0 X 0 0 0 0 0 0
XOR = Arr[0] ^ Arr[1] ^ Arr[2] = 1 1 0 0, Here there are odd number of 1‘s at 2nd and 3rd bits.
- So, add 2pow(2nd) to all elements of arr and in X, then again will take the XOR, after this the elements become:
5th | 4th | 3rd | 2nd | 1st | 0th | |
---|---|---|---|---|---|---|
arr[0] | 0 | 0 | 1 | 0 | 0 | 0 |
arr[1] | 0 | 0 | 1 | 0 | 0 | 1 |
arr[2] | 0 | 1 | 0 | 0 | 0 | 1 |
XOR | 0 | 1 | 0 | 0 | 0 | 0 |
X | 0 | 0 | 0 | 1 | 0 | 0 |
If this keeps on going the left most 1 in XOR keeps on moving left.
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find required result
long long solve(vector& a,
int n)
{
long long res = 0, j = 0, one = 1;
// For 64 Bit
while (j < 64) {
// j is traversing each bit
long long Xor = 0;
long long powerOf2 = one << j;
for (auto x : a)
Xor ^= x;
if (j == 63 && (Xor & powerOf2))
return -1;
if (Xor & powerOf2) {
res += powerOf2;
for (int i = 0; i < n; i++)
a[i] += powerOf2;
}
j++;
}
return res;
}
// Driver Code
int main()
{
// Size of arr[]
int N = 3;
vector arr = { 2, 4, 5 };
cout << solve(arr, N) << '\n';
return 0;
}
Java
// Java program for above approach
class GFG{
// Function to find required result
static long solve(int[] a,
int n)
{
long res = 0, j = 0, one = 1;
// For 64 Bit
while (j < 64)
{
// j is traversing each bit
long Xor = 0;
long powerOf2 = one << j;
for (int x : a)
Xor ^= x;
if (j == 63 && (Xor & powerOf2)!=0)
return -1;
if ((Xor & powerOf2)!=0) {
res += powerOf2;
for (int i = 0; i < n; i++)
a[i] += powerOf2;
}
j++;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
// Size of arr[]
int N = 3;
int[] arr = { 2, 4, 5 };
System.out.print(solve(arr, N));
}
}
// This code is contributed by shikhasingrajput
Python3
# python program for above approach
# Function to find required result
def solve(a, n):
res = 0
j = 0
one = 1
# For 64 Bit
while (j < 64):
# j is traversing each bit
Xor = 0
powerOf2 = one << j
for x in a:
Xor ^= x
if (j == 63 and (Xor & powerOf2)):
return -1
if (Xor & powerOf2):
res += powerOf2
for i in range(0, n):
a[i] += powerOf2
j += 1
return res
# Driver Code
if __name__ == "__main__":
# Size of arr[]
N = 3
arr = [2, 4, 5]
print(solve(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for above approach
using System;
class GFG
{
// Function to find required result
static long solve(int[] a, int n)
{
int res = 0, j = 0, one = 1;
// For 64 Bit
while (j < 64)
{
// j is traversing each bit
long Xor = 0;
long powerOf2 = one << j;
foreach (int x in a)
Xor ^= x;
if (j == 63 && (Xor & powerOf2) != 0)
return -1;
if ((Xor & powerOf2) != 0)
{
res += (int)powerOf2;
for (int i = 0; i < n; i++)
a[i] += (int)powerOf2;
}
j++;
}
return res;
}
// Driver Code
public static void Main()
{
// Size of arr[]
int N = 3;
int[] arr = { 2, 4, 5 };
Console.Write(solve(arr, N));
}
}
// This code is contributed by gfgking
Javascript
1
时间复杂度: O(N*logN)
辅助空间: O(1)