Recuento de tripletes que satisfacen la ecuación dada

Dada una array arr[] de N enteros no negativos. La tarea es contar el número de tripletes (i, j, k) donde 0 ≤ i < j ≤ k < N tal que A[i] ^ A[i + 1] ^ … ^ A[j – 1] = A [j] ^ A[j + 1] ^ … ^ A[k] donde ^ es el XOR bit a bit.
Ejemplos: 
 

Entrada: arr[] = {2, 5, 6, 4, 2} 
Salida:
Los tripletes válidos son (2, 3, 4) y (2, 4, 4).
Entrada: arr[] = {5, 2, 7} 
Salida:
 

Enfoque ingenuo: considere todos y cada uno de los tripletes y verifique si el xor de los elementos requeridos es igual o no.
Enfoque eficiente: Si arr[i] ^ arr[i + 1] ^ … ^ arr[j – 1] = arr[j] ^ arr[j + 1] ^ … ^ arr[k] entonces arr[i] ^ arr [i + 1] ^ … ^ arr[k] = 0 ya que X ^ X = 0 . Ahora el problema se reduce a encontrar los subconjuntos con XOR 0. Pero cada subconjunto puede tener múltiples tripletes, es decir 
 

Si arr[i] ^ arr[i + 1] ^ … ^ arr[k] = 0 
entonces, (arr[i]) ^ (arr[i + 1] ^ … ^ arr[k]) = 0 
y, arr [i] ^ (arr[i + 1]) ^ … ^ arr[k] = 0 
arr[i] ^ arr[i + 1] ^ (arr[i + 2]) ^ … ^ arr[k] = 0 
… 
j puede tener cualquier valor desde i + 1 hasta k sin violar la propiedad requerida. 
 

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

Javascript

<script>
// Javascript implementation of the approach
 
// Function to return the count
// of required triplets
function CountTriplets(arr, n)
{
    let ans = 0;
    for (let i = 0; i < n - 1; i++) {
 
        // First element of the
        // current sub-array
        let first = arr[i];
        for (let 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
    let arr = [ 2, 5, 6, 4, 2 ];
    let n = arr.length;
 
    document.write(CountTriplets(arr, n));
 
// This code is contributed by gfgking.
</script>
Producción: 

2

 

Complejidad Temporal: O(n 2 ). 
Espacio Auxiliar : O(1).  

Publicación traducida automáticamente

Artículo escrito por Shivamj075 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *