给定一个包含N个元素的数组arr [] ,任务是计算所有元素的XOR等于该子数组中所有元素之和的子数组的数量。
例子:
Input: arr[] = {2, 5, 4, 6}
Output: 5
Explanation:
All the subarrays {{2}, {5}, {4}, {6}} satisfies the above condition since the XOR of the subarrays is same as the sum. Apart from these, the subarray {2, 5} also satisfies the condition:
(2 xor 5) = 7 = (2 + 5)
Input: arr[] = {1, 2, 3, 4, 5}
Output: 7
幼稚的方法:针对此问题的幼稚的方法是考虑所有子数组,对于每个子数组,检查XOR是否等于和。
时间复杂度: O(N 2 )
高效的方法:想法是使用滑动窗口的概念。首先,我们计算满足上述条件的窗口,然后滑过每个元素直到N。可以按照以下步骤计算答案:
- 维持留下了两个三分球,右最初被分配到零。
- 使用满足条件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
输出:
7
时间复杂度: O(N) ,其中N是数组的长度。