给定一个整数数组Arr 。任务是计算三元组(i, j, k) 的数量,使得A i ^ A i+1 ^ A i+2 ^ …。 ^ A j-1 = A j ^ A j+1 ^ A j+2 ^ ….. ^ A k ,并且0 < (i, j, k) < N ,其中N是数组的大小。
其中^是两个数字的按位异或。
例子:
Input: Arr = [5, 2, 7]
Output: 2
Explanation:
The triplets are (0, 2, 2) since 5 ^ 2 = 7 and (0, 1, 2) since 2 ^ 7 = 5.
Input: Arr = [3, 6, 12, 8, 6, 2, 1, 5]
Output: 6
方法
- 让我们简化给定的表达式:
Ai ^ Ai + 1 ^ ...Aj - 1 = Aj ^ Aj + 1 ^ ...Ak
Taking XOR with Aj ^ Aj + 1 ^ ...Ak on both sides
Ai ^ Ai + 1 ^ ...Aj - 1 ^ Aj ^ Aj + 1 ^ ...Ak = 0
- 因此,具有 XOR 0 的子数组 [i, k] 将具有k – i 个三元组,因为可以从 [i+1, k] 中选择任意 j,并且该三元组将满足条件。
- 现在的问题简化为找出 XOR 为 0 的所有子数组的长度,并且对于每个这样的长度 L,将 L – 1 添加到答案中。
- 在前缀 XOR 数组中,如果在 2 个索引处,例如 L 和 R,前缀 XOR 值相同 – 那么这意味着子数组 [L + 1, R] = 0 的 XOR。
- 为了找到计数,我们将存储以下内容:
Say we are at index i, and the prefix XOR at i = x.
count[x] = Frequency of x in prefix XOR array before i
ways[x] -> For each all j < i, if prefixXor[j] = x,
then ways[x] += (j+1)
- 这些可用于使用以下公式计算三胞胎:
Triplets += count[x] * i - ways[x]
下面是上述方法的实现:
C++
// C++ program to count the Number of
// triplets in array having subarray
// XOR equal
#include
using namespace std;
// Function return the count of
// triplets having subarray XOR equal
int CountOfTriplets(int a[], int n)
{
int answer = 0;
// XOR value till i
int x = 0;
// Count and ways array as defined
// above
int count[100005] = { 0 };
int ways[100005] = { 0 };
for (int i = 0; i < n; i++)
{
x ^= a[i];
// Using the formula stated
answer += count[x] * i - ways[x];
// Increase the frequency of x
count[x]++;
// Add i+1 to ways[x] for upcoming
// indices
ways[x] += (i + 1);
}
return answer;
}
// Driver code
int main()
{
int Arr[] = { 3, 6, 12, 8, 6, 2, 1, 5 };
int N = sizeof(Arr) / sizeof(Arr[0]);
cout << CountOfTriplets(Arr, N);
return 0;
}
Java
// Java program to count the Number of
// triplets in array having subarray
// XOR equal
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.*;
import java.util.Collections;
class GFG
{
// Function return the count of
// triplets having subarray XOR equal
static int CountOfTriplets(int a[], int n)
{
int answer = 0;
// XOR value till i
int x = 0;
// Count and ways array as defined
// above
int count[] = new int[100005];
int ways[] = new int[100005];
for (int i = 0; i < n; i++)
{
x ^= a[i];
// Using the formula stated
answer += count[x] * i - ways[x];
// Increase the frequency of x
count[x]++;
// Add i+1 to ways[x] for upcoming
// indices
ways[x] += (i + 1);
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int Arr[] = { 3, 6, 12, 8, 6, 2, 1, 5 };
int N = Arr.length;
System.out.print(CountOfTriplets(Arr, N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to count the Number of
# triplets in array having subarray
# XOR equal
# Function return the count of
# triplets having subarray XOR equal
def CountOfTriplets(a,n):
answer = 0
# XOR value till i
x = 0
# Count and ways array as defined
# above
count = [0 for i in range(100005)]
ways = [0 for i in range(100005)]
for i in range(n):
x ^= a[i]
# Using the formula stated
answer += count[x] * i - ways[x]
# Increase the frequency of x
count[x] += 1
# Add i+1 to ways[x] for upcoming
# indices
ways[x] += (i + 1)
return answer
# Driver code
if __name__ == '__main__':
Arr = [3, 6, 12, 8, 6, 2, 1, 5]
N = len(Arr)
print(CountOfTriplets(Arr, N))
# This code is contributed by Bhupendra_Singh
C#
// C# program to count the Number of
// triplets in array having subarray
// XOR equal
using System;
class GFG {
// Function return the count of
// triplets having subarray XOR equal
static int CountOfTriplets(int []a, int n)
{
int answer = 0;
// XOR value till i
int x = 0;
// Count and ways array as defined
// above
int []count = new int[100005];
int []ways = new int[100005];
for(int i = 0; i < n; i++)
{
x ^= a[i];
// Using the formula stated
answer += count[x] * i - ways[x];
// Increase the frequency of x
count[x]++;
// Add i+1 to ways[x] for upcoming
// indices
ways[x] += (i + 1);
}
return answer;
}
// Driver code
public static void Main(String[] args)
{
int []Arr = { 3, 6, 12, 8, 6, 2, 1, 5 };
int N = Arr.Length;
Console.Write(CountOfTriplets(Arr, N));
}
}
// This code is contributed by Rohit_ranjan
Javascript
输出:
6
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。