📌  相关文章
📜  计算总和等于其 XOR 值的子数组

📅  最后修改于: 2021-09-06 06:40:12             🧑  作者: Mango

给定一个包含N 个元素的数组arr[] ,任务是计算所有元素的异或等于子数组中所有元素之和的子数组的数量。
例子:

朴素的方法:这个问题的朴素的方法是考虑所有的子数组和每个子数组,检查 XOR 是否等于总和。
时间复杂度: O(N 2 )
高效的方法:这个想法是使用滑动窗口的概念。首先,我们计算满足上述条件的窗口,然后我们滑动每个元素直到 N。可以按照以下步骤计算答案:

  • 保持两个指针leftright最初分配为零。
  • 使用满足条件 A xor B = A + B 的右指针计算窗口。
  • 子数组的计数将是right – left
  • 遍历每个元素并删除前一个元素。

下面是上述方法的实现:

C++
// C++ program to count the number
// of subarrays such that Xor of
// all the elements of that subarray
// is equal to sum of the elements
 
#include 
#define ll long long int
using namespace std;
 
// Function to count the number
// of subarrays such that Xor of
// all the elements of that subarray
// is equal to sum of the elements
ll operation(int arr[], int N)
{
    // Maintain two pointers
    // left and right
    ll right = 0, ans = 0,
       num = 0;
 
    // Iterating through the array
    for (ll left = 0; left < N; left++) {
 
        // Calculate the window
        // where the above condition
        // is satisfied
        while (right < N
               && num + arr[right]
                      == (num ^ arr[right])) {
            num += arr[right];
            right++;
        }
 
        // Count will be (right-left)
        ans += right - left;
        if (left == right)
            right++;
 
        // Remove the previous element
        // as it is already included
        else
            num -= arr[left];
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << operation(arr, N);
}


Java
// Java program to count the number
// of subarrays such that Xor of all
// the elements of that subarray is
// equal to sum of the elements
import java.io.*;
 
class GFG{
     
// Function to count the number
// of subarrays such that Xor of
// all the elements of that subarray
// is equal to sum of the elements
static long operation(int arr[], int N)
{
     
    // Maintain two pointers
    // left and right
    int right = 0;
    int    num = 0;
    long ans = 0;
 
    // Iterating through the array
    for(int left = 0; left < N; left++)
    {
        
       // Calculate the window
       // where the above condition
       // is satisfied
       while (right < N && num + arr[right] ==
                          (num ^ arr[right]))
       {
           num += arr[right];
           right++;
       }
        
       // Count will be (right-left)
       ans += right - left;
       if (left == right)
           right++;
        
       // Remove the previous element
       // as it is already included
       else
           num -= arr[left];
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = arr.length;
 
    System.out.println(operation(arr, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to count the number
# of subarrays such that Xor of
# all the elements of that subarray
# is equal to sum of the elements
 
# Function to count the number
# of subarrays such that Xor of
# all the elements of that subarray
# is equal to sum of the elements
def operation(arr, N):
 
    # Maintain two pointers
    # left and right
    right = 0; ans = 0;
    num = 0;
 
    # Iterating through the array
    for left in range(0, N):
 
        # Calculate the window
        # where the above condition
        # is satisfied
        while (right < N and
               num + arr[right] ==
              (num ^ arr[right])):
            num += arr[right];
            right += 1;
 
        # Count will be (right-left)
        ans += right - left;
        if (left == right):
            right += 1;
 
        # Remove the previous element
        # as it is already included
        else:
            num -= arr[left];
 
    return ans;
 
# Driver code
arr = [1, 2, 3, 4, 5];
N = len(arr)
print(operation(arr, N));
 
# This code is contributed by Nidhi_biet


C#
// C# program to count the number
// of subarrays such that Xor of all
// the elements of that subarray is
// equal to sum of the elements
using System;
class GFG{
     
// Function to count the number
// of subarrays such that Xor of
// all the elements of that subarray
// is equal to sum of the elements
static long operation(int []arr, int N)
{
     
    // Maintain two pointers
    // left and right
    int right = 0;
    int num = 0;
    long ans = 0;
 
    // Iterating through the array
    for(int left = 0; left < N; left++)
    {
         
        // Calculate the window
        // where the above condition
        // is satisfied
        while (right < N &&
               num + arr[right] ==
              (num ^ arr[right]))
        {
            num += arr[right];
            right++;
        }
             
        // Count will be (right-left)
        ans += right - left;
        if (left == right)
            right++;
             
        // Remove the previous element
        // as it is already included
        else
            num -= arr[left];
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
 
    Console.WriteLine(operation(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
7

时间复杂度: O(N) ,其中 N 是数组的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live