📜  长度为K的所有子阵列的XOR之和

📅  最后修改于: 2021-05-08 16:28:24             🧑  作者: Mango

给定一个长度为n(n> k)的数组,我们必须找到长度为k的子数组的所有元素的xor之和。

例子:

天真的解决方案:这个想法是遍历所有长度为k的子数组,找到子数组的所有元素的异或,并将它们求和,以求出数组中所有K个长度子数组的XOR之和。
时间复杂度:O(N 2 )

有效的解决方案:有效的解决方案是遍历数组并找到所有长度为k的子数组,即(0到k-1),(1到k),(2到k + 1),…。(n-k +1到n)。

我们将通过形成pre-xor数组来查找并存储从0到i的元素的xor(在数组x []中)。

现在,从l到r的子数组的xor等于x [l-1] ^ x [r],因为x [r]将给出所有元素的xor,直到r和x [l-1]将给出x的xor。所有元素直到l-1。当我们对这两个值进行异或运算时,将重复直到0到l-1的元素。当a ^ a = 0时,重复值将为净值贡献零,并且我们得到从x到r的xor子数组的值。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Sum of XOR of all K length
// sub-array of an array
int FindXorSum(int arr[], int k, int n)
{
    // If the length of the array is less than k
    if (n < k)
        return 0;
  
    // Array that will store xor values of
    // subarray from 1 to i
    int x[n] = { 0 };
    int result = 0;
  
    // Traverse through the array
    for (int i = 0; i < n; i++) {
  
        // If i is greater than zero, store
        // xor of all the elements from 0 to i
        if (i > 0)
            x[i] = x[i - 1] ^ arr[i];
  
        // If it is the first element
        else
            x[i] = arr[i];
  
        // If i is greater than k
        if (i >= k - 1) {
            int sum = 0;
  
            // Xor of values from 0 to i
            sum = x[i];
  
            // Now to find subarray of length k
            // that ends at i, xor sum with x[i-k]
            if (i - k > -1)
                sum ^= x[i - k];
  
            // Add the xor of elements from i-k+1 to i
            result += sum;
        }
    }
  
    // Return the resultant sum;
    return result;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
  
    int n = 4, k = 2;
  
    cout << FindXorSum(arr, k, n) << endl;
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Sum of XOR of all K length
// sub-array of an array
static int FindXorSum(int arr[], int k, int n)
{
    // If the length of the array is less than k
    if (n < k)
        return 0;
  
    // Array that will store xor values of
    // subarray from 1 to i
    int []x = new int[n];
    int result = 0;
  
    // Traverse through the array
    for (int i = 0; i < n; i++) 
    {
  
        // If i is greater than zero, store
        // xor of all the elements from 0 to i
        if (i > 0)
            x[i] = x[i - 1] ^ arr[i];
  
        // If it is the first element
        else
            x[i] = arr[i];
  
        // If i is greater than k
        if (i >= k - 1)
        {
            int sum = 0;
  
            // Xor of values from 0 to i
            sum = x[i];
  
            // Now to find subarray of length k
            // that ends at i, xor sum with x[i-k]
            if (i - k > -1)
                sum ^= x[i - k];
  
            // Add the xor of elements from i-k+1 to i
            result += sum;
        }
    }
  
    // Return the resultant sum;
    return result;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 2, 3, 4 };
  
    int n = 4, k = 2;
  
    System.out.println(FindXorSum(arr, k, n));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python implementation of above approach 
  
# Sum of XOR of all K length 
# sub-array of an array 
def FindXorSum(arr, k, n): 
      
    # If the length of the array is less than k 
    if (n < k): 
        return 0; 
  
    # Array that will store xor values of 
    # subarray from 1 to i 
    x = [0]*n; 
    result = 0; 
  
    # Traverse through the array 
    for i in range(n):
  
        # If i is greater than zero, store 
        # xor of all the elements from 0 to i 
        if (i > 0): 
            x[i] = x[i - 1] ^ arr[i]; 
  
        # If it is the first element 
        else:
            x[i] = arr[i]; 
  
        # If i is greater than k 
        if (i >= k - 1):
            sum = 0; 
  
            # Xor of values from 0 to i 
            sum = x[i]; 
  
            # Now to find subarray of length k 
            # that ends at i, xor sum with x[i-k] 
            if (i - k > -1): 
                sum ^= x[i - k]; 
  
            # Add the xor of elements from i-k+1 to i 
            result += sum; 
  
    # Return the resultant sum; 
    return result; 
  
# Driver code
arr = [ 1, 2, 3, 4 ]; 
  
n = 4; k = 2; 
  
print(FindXorSum(arr, k, n)); 
  
# This code has been contributed by 29AjayKumar


C#
// C# implementation of the above approach 
using System;
  
class GFG 
{ 
      
    // Sum of XOR of all K length 
    // sub-array of an array 
    static int FindXorSum(int []arr, int k, int n) 
    { 
        // If the length of the array is less than k 
        if (n < k) 
            return 0; 
      
        // Array that will store xor values of 
        // subarray from 1 to i 
        int []x = new int[n]; 
        int result = 0; 
      
        // Traverse through the array 
        for (int i = 0; i < n; i++) 
        { 
      
            // If i is greater than zero, store 
            // xor of all the elements from 0 to i 
            if (i > 0) 
                x[i] = x[i - 1] ^ arr[i]; 
      
            // If it is the first element 
            else
                x[i] = arr[i]; 
      
            // If i is greater than k 
            if (i >= k - 1) 
            { 
                int sum = 0; 
      
                // Xor of values from 0 to i 
                sum = x[i]; 
      
                // Now to find subarray of length k 
                // that ends at i, xor sum with x[i-k] 
                if (i - k > -1) 
                    sum ^= x[i - k]; 
      
                // Add the xor of elements from i-k+1 to i 
                result += sum; 
            } 
        } 
      
        // Return the resultant sum; 
        return result; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int []arr = { 1, 2, 3, 4 }; 
      
        int n = 4, k = 2; 
      
        Console.WriteLine(FindXorSum(arr, k, n)); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
11

时间复杂度:O(N)