📌  相关文章
📜  求一个数 X,使得给定数组的每个元素加上 X 后的 XOR 为 0

📅  最后修改于: 2022-05-13 01:56:08.293000             🧑  作者: Mango

求一个数 X,使得给定数组的每个元素加上 X 后的 XOR 为 0

给定一个包含正整数的奇数长度 N数组arr[] 。任务是找到一个正整数X ,这样,将X添加到arr[]的所有元素,然后对所有元素进行XOR得到0 。如果不存在这样的X ,则返回-1

例子:

方法:奇数个1的XOR = 1,而偶数个1 = 0。这个想法可以用来解决给定的问题。请按照以下步骤操作:

  • 初始化变量X = 0
  • 数组元素的二进制表示将用于遍历元素并确定X
  • 从第 0 位开始遍历到第 63 位。
  • 如果在任何位位置,数组元素的设置位 (1) 总数为奇数,则将 2 的幂与 X 相加。
  • 如果在迭代完成后在任何位位置有奇数个 1,则不存在这样的 X。否则,打印 X 作为答案。

请参见下面的插图:

在不可能的情况下采取以下 X

下面是上述方法的实现:

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)