📌  相关文章
📜  满足给定方程的三元组计数

📅  最后修改于: 2021-04-24 18:10:01             🧑  作者: Mango

给定N个非负整数的数组arr [] 。任务是计算三元组(i,j,k)的数量,其中0≤i ,使得A [i] ^ A [i + 1] ^…^ A [j – 1] = A [j] ^ A [j + 1] ^…^ A [k]其中^是按位XOR。

例子:

天真的方法:考虑每个三元组,并检查所需元素的异或是否相等。

高效的方法:如果arr [i] ^ arr [i + 1] ^…^ arr [j – 1] = arr [j] ^ arr [j + 1] ^…^ arr [k]arr [i] ^ arr [i + 1] ^ … ^ arr [k] = 0,因为X ^ X = 0 。现在问题被简化为找到XOR为0的子数组。但是每个这样的子数组都可以有多个这样的三元组,即

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count
// of required triplets
int CountTriplets(int* arr, int n)
{
    int ans = 0;
    for (int i = 0; i < n - 1; i++) {
  
        // First element of the
        // current sub-array
        int first = arr[i];
        for (int j = i + 1; j < n; j++) {
  
            // XOR every element of
            // the current sub-array
            first ^= arr[j];
  
            // If the XOR becomes 0 then
            // update the count of triplets
            if (first == 0)
                ans += (j - i);
        }
    }
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 5, 6, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << CountTriplets(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to return the count
// of required triplets
static int CountTriplets(int[] arr, int n)
{
    int ans = 0;
    for (int i = 0; i < n - 1; i++)
    {
  
        // First element of the
        // current sub-array
        int first = arr[i];
        for (int j = i + 1; j < n; j++) 
        {
  
            // XOR every element of
            // the current sub-array
            first ^= arr[j];
  
            // If the XOR becomes 0 then
            // update the count of triplets
            if (first == 0)
                ans += (j - i);
        }
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = {2, 5, 6, 4, 2};
    int n = arr.length;
  
    System.out.println(CountTriplets(arr, n));
}
} 
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
  
# Function to return the count
# of required triplets
def CountTriplets(arr, n):
  
    ans = 0
    for i in range(n - 1):
  
        # First element of the
        # current sub-array
        first = arr[i]
        for j in range(i + 1, n):
  
            # XOR every element of
            # the current sub-array
            first ^= arr[j]
  
            # If the XOR becomes 0 then
            # update the count of triplets
            if (first == 0):
                ans += (j - i)
  
    return ans
  
# Driver code
arr = [2, 5, 6, 4, 2 ]
n = len(arr)
print(CountTriplets(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function to return the count
    // of required triplets
    static int CountTriplets(int[] arr, int n)
    {
        int ans = 0;
        for (int i = 0; i < n - 1; i++)
        {
      
            // First element of the
            // current sub-array
            int first = arr[i];
            for (int j = i + 1; j < n; j++) 
            {
      
                // XOR every element of
                // the current sub-array
                first ^= arr[j];
      
                // If the XOR becomes 0 then
                // update the count of triplets
                if (first == 0)
                    ans += (j - i);
            }
        }
        return ans;
    }
      
    // Driver code
    public static void Main()
    {
        int []arr = {2, 5, 6, 4, 2};
        int n = arr.Length;
      
        Console.WriteLine(CountTriplets(arr, n));
    }
}
  
// This code is contributed by AnkitRai01


输出:
2

时间复杂度: O(n 2 )