📜  所有子数组XOR的XOR |套装2

📅  最后修改于: 2021-05-04 23:22:59             🧑  作者: Mango

给定一个整数数组,我们需要获得所有子数组XOR的总XOR,其中子数组XOR可以通过对其所有元素进行XOR运算获得。
例子 :

Input : arr[] = [3, 5, 2, 4, 6]
Output : 7
Total XOR of all subarray XORs is,
(3) ^ (5) ^ (2) ^ (4) ^ (6)
(3^5) ^ (5^2) ^ (2^4) ^ (4^6)
(3^5^2) ^ (5^2^4) ^ (2^4^6)
(3^5^2^4) ^ (5^2^4^6) ^
(3^5^2^4^6) = 7     

Input : arr[] = {1, 2, 3, 4}
Output : 0
Total XOR of all subarray XORs is,
(1) ^ (2) ^ (3) ^ (4) ^
(1^2) ^ (2^3) ^ (3^4) ^ 
(1^2^3) ^ (2^3^4) ^
(1^2^3^4) = 0

我们在下面的文章中讨论了O(n)解决方案。
所有子数组XOR的XOR |套装1
如上文所述,第i个索引处的元素频率由(i + 1)*(Ni)给出,其中N是数组的大小
可能有4种情况:
情况1:我是奇数,N是奇数
令i = 2k + 1,N = 2m + 1
freq [i] =(((2k + 1)+1)*((2m + 1)-(2k + 1))= 4(mk)(k + 1)=偶数
情况2:我是奇数,N是偶数
令i = 2k + 1,N = 2m
freq [i] =(((2k + 1)+1)*((2m)-(2k + 1))= 2(k + 1)(2m-2k-1)=偶数
情况3:我是偶数,N是奇数
令i = 2k,N = 2m + 1
freq [i] =(((2k)+1)*((2m + 1)-(2k))= 2k(2m-2k + 1)+(2m-2k)+1 =奇数
情况4:我是偶数,N是偶数
令i = 2k,N = 2m
freq [i] =((2k)+1)*((2m)-(2k))= 2(mk)(2k + 1)=偶数
由此可以得出结论,如果数组中元素的总数为偶数,则在任何位置的元素的频率都是偶数。因此,总异或将为0。元素的个数为奇数,则偶数位置的元素的频率为奇数,加法位置为偶数。因此,我们只需要查找偶数位置元素的XOR。
以下是上述想法的实现:

C++
// C++ program to get total xor of all subarray xors
#include 
using namespace std;
 
// Returns XOR of all subarray xors
int getTotalXorOfSubarrayXors(int arr[], int N)
{
    // if even number of terms are there, all
    // numbers will appear even number of times.
    // So result is 0.
    if (N % 2 == 0)
       return 0;
 
    // else initialize result by 0 as (a xor 0 = a)
    int res = 0;
    for (int i = 0; i


Java
// Java program to get total
// xor of all subarray xors
import java.io.*;
 
class GFG
{
    // Returns XOR of all
    // subarray xors
    static int getTotalXorOfSubarrayXors(int arr[],
                                         int N)
    {
         
    // if even number of terms are
    // there, all numbers will appear
    // even number of times. So result is 0.
    if (N % 2 == 0)
    return 0;
 
    // else initialize result
    // by 0 as (a xor 0 = a)
    int res = 0;
    for (int i = 0; i < N; i += 2)
        res ^= arr[i];
 
    return res;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
    int arr[] = {3, 5, 2, 4, 6};
    int N = arr.length;
         
    System.out.println(
            getTotalXorOfSubarrayXors(arr, N));
    }
}
 
// This code is contributed by ajit


Python 3
# Python 3 program to get total xor
# of all subarray xors
 
# Returns XOR of all subarray xors
def getTotalXorOfSubarrayXors(arr, N):
 
    # if even number of terms are there,
    # all numbers will appear even number
    # of times. So result is 0.
    if (N % 2 == 0):
        return 0
 
    # else initialize result by 0
    # as (a xor 0 = a)
    res = 0
    for i in range(0, N, 2):
        res ^= arr[i]
 
    return res
 
# Driver code
if __name__ == "__main__":
 
    arr = [3, 5, 2, 4, 6]
    N = len(arr)
    print(getTotalXorOfSubarrayXors(arr, N))
 
# This code is contributed by ita_c


C#
// C# program to get total
// xor of all subarray xors
using System;
 
class GFG
{
     
    // Returns XOR of all
    // subarray xors
    static int getTotalXorOfSubarrayXors(int []arr,
                                         int N)
    {
         
    // if even number of terms
    // are there, all numbers
    // will appear even number
    // of times. So result is 0.
    if (N % 2 == 0)
    return 0;
 
    // else initialize result
    // by 0 as (a xor 0 = a)
    int res = 0;
    for (int i = 0; i < N; i += 2)
        res ^= arr[i];
 
    return res;
    }
     
    // Driver Code
    static void Main()
    {
    int []arr = {3, 5, 2, 4, 6};
    int N = arr.Length;
    Console.Write(getTotalXorOfSubarrayXors(arr, N));
}
}
 
// This code is contributed by aj_36


PHP


Javascript


输出:

7