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

📅  最后修改于: 2021-04-23 07:57:21             🧑  作者: 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}
Output : 2

Input : arr[] = {1, 2, 3, 4}
Output : 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)
{
    //  initialize result by 0 as (a xor 0 = a)
    int res = 0;
 
    // select the starting element
    for (int i=0; i


Java
// java program to get total XOR
// of all subarray xors
public class GFG {
         
    // Returns XOR of all subarray xors
    static int getTotalXorOfSubarrayXors(
                          int arr[], int N)
    {
         
        // initialize result by
        // 0 as (a xor 0 = a)
        int res = 0;
         
        // select the starting element
        for (int i = 0; i < N; i++)
         
            // select the eNding element
            for (int j = i; j < N; j++)
         
            // Do XOR of elements
            // in current subarray
            for (int k = i; k <= j; k++)
                res = res ^ arr[k];
     
        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 Sam007.


Python 3
# python program to get total xor
# of all subarray xors
 
# Returns XOR of all subarray xors
def getTotalXorOfSubarrayXors(arr, N):
     
    # initialize result by 0 as
    # (a xor 0 = a)
    res = 0
 
    # select the starting element
    for i in range(0, N):
         
        # select the eNding element
        for j in range(i, N):
             
            # Do XOR of elements in
            # current subarray
            for k in range(i, j + 1):
                res = res ^ arr[k]
             
    return res
 
# Driver code to test above methods
arr = [3, 5, 2, 4, 6]
N = len(arr)
 
print(getTotalXorOfSubarrayXors(arr, N))
 
# This code is contributed by Sam007.


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)
{
     
// initialize result by
// 0 as (a xor 0 = a)
int res = 0;
 
// select the starting element
for (int i = 0; i < N; i++)
 
    // select the eNding element
    for (int j = i; j < N; j++)
 
        // Do XOR of elements
        // in current subarray
        for (int k = i; k <= j; k++)
            res = res ^ arr[k];
 
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 Sam007


PHP


Javascript


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)
{
    // initialize result by 0
    // as (a XOR 0 = a)
    int res = 0;
 
    // loop over all elements once
    for (int i = 0; i < N; i++)
    {
        // get the frequency of
        // current element
        int freq = (i + 1) * (N - i);
 
        // Uncomment below line to print
        // the frequency of arr[i]
        // cout << arr[i] << " " << freq << endl;
 
        // if frequency is odd, then
        // include it in the result
        if (freq % 2 == 1)
            res = res ^ arr[i];
    }
 
    // return the result
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = {3, 5, 2, 4, 6};
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << getTotalXorOfSubarrayXors(arr, N);
    return 0;
}


Java
// java program to get total xor
// of all subarray xors
import java.io.*;
 
public class GFG {
     
    // Returns XOR of all subarray
    // xors
    static int getTotalXorOfSubarrayXors(
                          int arr[], int N)
    {
         
        // initialize result by 0
        // as (a XOR 0 = a)
        int res = 0;
     
        // loop over all elements once
        for (int i = 0; i < N; i++)
        {
            // get the frequency of
            // current element
            int freq = (i + 1) * (N - i);
     
            // Uncomment below line to print
            // the frequency of arr[i]
             
            // if frequency is odd, then
            // include it in the result
            if (freq % 2 == 1)
                res = res ^ arr[i];
        }
     
        // return the result
        return res;
    }
     
    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 Sam007.


Python 3
# Python3 program to get total
# xor of all subarray xors
 
# Returns XOR of all
# subarray xors
def getTotalXorOfSubarrayXors(arr, N):
 
    # initialize result by 0
    # as (a XOR 0 = a)
    res = 0
 
    # loop over all elements once
    for i in range(0, N):
     
        # get the frequency of
        # current element
        freq = (i + 1) * (N - i)
 
        # Uncomment below line to print
        # the frequency of arr[i]
 
        # if frequency is odd, then
        # include it in the result
        if (freq % 2 == 1):
            res = res ^ arr[i]
     
    # return the result
    return res
 
# Driver Code
arr = [3, 5, 2, 4, 6]
N = len(arr)
print(getTotalXorOfSubarrayXors(arr, N))
     
# This code is contributed
# by Smitha


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)
{
    // initialize result by 0
    // as (a XOR 0 = a)
    int res = 0;
 
    // loop over all elements once
    for (int i = 0; i < N; i++)
    {
        // get the frequency of
        // current element
        int freq = (i + 1) * (N - i);
 
        // Uncomment below line to print
        // the frequency of arr[i]
         
        // if frequency is odd, then
        // include it in the result
        if (freq % 2 == 1)
            res = res ^ arr[i];
    }
 
    // return the result
    return res;
}
     
    // Driver Code
    public static void Main()
    {
    int []arr = {3, 5, 2, 4, 6};
    int N = arr.Length;
 
    Console.Write(getTotalXorOfSubarrayXors(arr, N));
    }
}
     
 
// This code is contributed by Sam007


PHP


输出:

7

时间复杂度:O(N 3 )
一个有效的解决方案是基于枚举所有子数组的思想,我们可以计算所有子数组中每个元素的出现频率,如果一个元素的频率为奇数,则它将被包括在最终结果中,否则将不包括在内。

As in above example, 
3 occurred 5 times,
5 occurred 8 times,
2 occurred 9 times,
4 occurred 8 times,
6 occurred 5 times
So our final result will be xor of all elements which occurred odd number of times
i.e. 3^2^6 = 7

From above occurrence pattern we can observe that number at i-th index will have 
(i + 1) * (N - i) frequency. 

因此,我们可以遍历所有元素一次并计算它们的频率,如果它是奇数,则可以通过将其与结果进行异或运算将其包括在最终结果中。
解决方案的总时间复杂度为O(N)

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)
{
    // initialize result by 0
    // as (a XOR 0 = a)
    int res = 0;
 
    // loop over all elements once
    for (int i = 0; i < N; i++)
    {
        // get the frequency of
        // current element
        int freq = (i + 1) * (N - i);
 
        // Uncomment below line to print
        // the frequency of arr[i]
        // cout << arr[i] << " " << freq << endl;
 
        // if frequency is odd, then
        // include it in the result
        if (freq % 2 == 1)
            res = res ^ arr[i];
    }
 
    // return the result
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = {3, 5, 2, 4, 6};
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << getTotalXorOfSubarrayXors(arr, N);
    return 0;
}

Java

// java program to get total xor
// of all subarray xors
import java.io.*;
 
public class GFG {
     
    // Returns XOR of all subarray
    // xors
    static int getTotalXorOfSubarrayXors(
                          int arr[], int N)
    {
         
        // initialize result by 0
        // as (a XOR 0 = a)
        int res = 0;
     
        // loop over all elements once
        for (int i = 0; i < N; i++)
        {
            // get the frequency of
            // current element
            int freq = (i + 1) * (N - i);
     
            // Uncomment below line to print
            // the frequency of arr[i]
             
            // if frequency is odd, then
            // include it in the result
            if (freq % 2 == 1)
                res = res ^ arr[i];
        }
     
        // return the result
        return res;
    }
     
    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 Sam007.

的Python 3

# Python3 program to get total
# xor of all subarray xors
 
# Returns XOR of all
# subarray xors
def getTotalXorOfSubarrayXors(arr, N):
 
    # initialize result by 0
    # as (a XOR 0 = a)
    res = 0
 
    # loop over all elements once
    for i in range(0, N):
     
        # get the frequency of
        # current element
        freq = (i + 1) * (N - i)
 
        # Uncomment below line to print
        # the frequency of arr[i]
 
        # if frequency is odd, then
        # include it in the result
        if (freq % 2 == 1):
            res = res ^ arr[i]
     
    # return the result
    return res
 
# Driver Code
arr = [3, 5, 2, 4, 6]
N = len(arr)
print(getTotalXorOfSubarrayXors(arr, N))
     
# This code is contributed
# by Smitha

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)
{
    // initialize result by 0
    // as (a XOR 0 = a)
    int res = 0;
 
    // loop over all elements once
    for (int i = 0; i < N; i++)
    {
        // get the frequency of
        // current element
        int freq = (i + 1) * (N - i);
 
        // Uncomment below line to print
        // the frequency of arr[i]
         
        // if frequency is odd, then
        // include it in the result
        if (freq % 2 == 1)
            res = res ^ arr[i];
    }
 
    // return the result
    return res;
}
     
    // Driver Code
    public static void Main()
    {
    int []arr = {3, 5, 2, 4, 6};
    int N = arr.Length;
 
    Console.Write(getTotalXorOfSubarrayXors(arr, N));
    }
}
     
 
// This code is contributed by Sam007

的PHP


输出 :

7