📌  相关文章
📜  计算按位XOR等于0的偶数长度子数组

📅  最后修改于: 2021-05-17 20:52:24             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是对子数组元素的按位XOR等于0的所有可能的偶数长度子数组进行计数。

例子:

天真的方法:最简单的方法是遍历数组并生成所有可能的子数组。对于每个子数组,请检查子数组的长度是否为偶数,以及子数组元素的按位XOR是否为0 。请按照以下步骤解决问题:

  • 初始化一个变量,例如res,以存储满足给定条件的子数组的数量。
  • 遍历数组并生成所有可能的子数组。
  • 对于每个子数组,请检查子数组的长度是否为偶数,以及它们的元素的按位XOR为0,然后将res增加1。
  • 最后,打印res的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the number
// of even-length subarrays
// having Bitwise XOR equal to 0
int cntSubarr(int arr[], int N)
{
    // Stores the count of
    // required subarrays
    int res = 0;
 
    // Stores prefix-XOR
    // of arr[i, i+1, ...N-1]
    int prefixXor = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1;
         i++) {
 
        prefixXor = arr[i];
 
        for (int j = i + 1; j < N;
             j++) {
 
            // Calculate the prefix-XOR
            // of current subarray
            prefixXor ^= arr[j];
 
            // Check if XOR of the
            // current subarray is 0
            // and length is even
            if (prefixXor == 0
                && (j - i + 1) % 2 == 0) {
                res++;
            }
        }
    }
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << cntSubarr(arr, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
  
// Function to count the number
// of even-length subarrays
// having Bitwise XOR equal to 0
static int cntSubarr(int arr[], int N)
{
     
    // Stores the count of
    // required subarrays
    int res = 0;
  
    // Stores prefix-XOR
    // of arr[i, i+1, ...N-1]
    int prefixXor = 0;
  
    // Traverse the array
    for(int i = 0; i < N - 1; i++)
    {
        prefixXor = arr[i];
  
        for(int j = i + 1; j < N; j++)
        {
             
            // Calculate the prefix-XOR
            // of current subarray
            prefixXor ^= arr[j];
  
            // Check if XOR of the
            // current subarray is 0
            // and length is even
            if (prefixXor == 0 &&
               (j - i + 1) % 2 == 0)
            {
                res++;
            }
        }
    }
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.length;
     
    System.out.println(cntSubarr(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to count the number
# of even-length subarrays
# having Bitwise XOR equal to 0
def cntSubarr(arr, N):
     
    # Stores the count of
    # required subarrays
    res = 0
 
    # Stores prefix-XOR
    # of arr[i, i+1, ...N-1]
    prefixXor = 0
 
    # Traverse the array
    for i in range(N - 1):
        prefixXor = arr[i]
        for j in range(i + 1, N):
 
            # Calculate the prefix-XOR
            # of current subarray
            prefixXor ^= arr[j]
 
            # Check if XOR of the
            # current subarray is 0
            # and length is even
            if (prefixXor == 0 and
               (j - i + 1) % 2 == 0):
                res += 1
                 
    return res
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, 2, 3, 3, 6, 7, 8 ]
    N = len(arr)
     
    print(cntSubarr(arr, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
  
class GFG{
   
// Function to count the number
// of even-length subarrays
// having Bitwise XOR equal to 0
static int cntSubarr(int[] arr, int N)
{
      
    // Stores the count of
    // required subarrays
    int res = 0;
   
    // Stores prefix-XOR
    // of arr[i, i+1, ...N-1]
    int prefixXor = 0;
   
    // Traverse the array
    for(int i = 0; i < N - 1; i++)
    {
        prefixXor = arr[i];
   
        for(int j = i + 1; j < N; j++)
        {
              
            // Calculate the prefix-XOR
            // of current subarray
            prefixXor ^= arr[j];
   
            // Check if XOR of the
            // current subarray is 0
            // and length is even
            if (prefixXor == 0 &&
               (j - i + 1) % 2 == 0)
            {
                res++;
            }
        }
    }
    return res;
}
   
// Driver Code
public static void Main ()
{
    int[] arr = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.Length;
      
    Console.WriteLine(cntSubarr(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Javascript


C++
// C++ program to implement
// the above appraoch
#include 
using namespace std;
#define M 1000000
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
int cntSubXor(int arr[], int N)
{
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int Even[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int Odd[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for (int i = 0; i < N; i++) {
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1) {
 
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else {
 
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << cntSubXor(arr, N);
    return 0;
}


Java
// Java program to implement
// the above appraoch
import java.util.*;
 
class GFG{
     
static final int M =  1000000;
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
static int cntSubXor(int arr[], int N)
{
     
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int []Even = new int[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int []Odd = new int[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for(int i = 0; i < N; i++)
    {
         
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1)
        {
             
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else
        {
             
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.length;
     
    System.out.print(cntSubXor(arr, N));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above appraoch
M = 1000000;
 
# Function to get the count
# of even length subarrays
# having bitwise xor 0
def cntSubXor(arr, N):
   
    # Stores prefix-xor of
    # the given array
    prefixXor = 0;
 
    # Stores prefix-xor at
    # even index of the array.
    Even =[0] * M;
 
    # Stores prefix-xor at
    # odd index of the array.
    Odd = [0] * M;
 
    # Stores count of subarrays
    # that satisfy the condition
    cntSub = 0;
 
    # length from 0 index
    # to odd index is even
    Odd[0] = 1;
 
    # Traverse the array.
    for i in range(0, N):
 
        # Take prefix-xor
        prefixXor ^= arr[i];
 
        # If index is odd
        if (i % 2 == 1):
 
            # Calculate pairs
            cntSub += Odd[prefixXor];
 
            # Increment prefix-xor
            # at odd index
            Odd[prefixXor] += 1;
        else:
 
            # Calculate pairs
            cntSub += Even[prefixXor];
 
            # Increment prefix-xor
            # at odd index
            Even[prefixXor] += 1;
 
    return cntSub;
 
# Driver Code
if __name__ == '__main__':
   
    arr = [2, 2, 3, 3,
           6, 7, 8];
    N = len(arr);
    print(cntSubXor(arr, N));
 
# This code is contributed by gauravrajput1


C#
// C# program to implement
// the above appraoch
using System;
 
class GFG{
     
static readonly int M =  1000000;
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
static int cntSubXor(int []arr, int N)
{
     
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int []Even = new int[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int []Odd = new int[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for(int i = 0; i < N; i++)
    {
         
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1)
        {
             
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else
        {
             
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.Length;
     
    Console.Write(cntSubXor(arr, N));
}
}
 
// This code is contributed by gauravrajput1


输出
3

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:可以使用哈希解决问题。想法是将Prefix Xor的频率存储在两个单独的数组中,例如Odd []Even [] ,以存储给定数组的奇数和偶数索引的Prefix XOR的频率。最后,打印来自Even []Odd []数组且值大于或等于2的所有可能对的计数。以下是观察结果:

请按照以下步骤解决问题:

  • 初始化两个数组,例如Even []Odd [] ,分别在给定数组的偶数和奇数索引处存储Prefix XOR的频率。
  • 初始化一个变量,例如cntSub ,以存储满足给定条件的子数组的数量。
  • 遍历给定数组并计算给定数组的Prefix Xor
  • 前缀XOR的频率分别存储在数组Even []Odd []中给定数组的偶数和奇数索引处。
  • 最后,打印值大于或等于2的所有可能的Even []Odd []对的计数。

下面是上述方法的实现:

C++

// C++ program to implement
// the above appraoch
#include 
using namespace std;
#define M 1000000
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
int cntSubXor(int arr[], int N)
{
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int Even[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int Odd[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for (int i = 0; i < N; i++) {
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1) {
 
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else {
 
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << cntSubXor(arr, N);
    return 0;
}

Java

// Java program to implement
// the above appraoch
import java.util.*;
 
class GFG{
     
static final int M =  1000000;
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
static int cntSubXor(int arr[], int N)
{
     
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int []Even = new int[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int []Odd = new int[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for(int i = 0; i < N; i++)
    {
         
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1)
        {
             
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else
        {
             
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.length;
     
    System.out.print(cntSubXor(arr, N));
}
}
 
// This code is contributed by Amit Katiyar

Python3

# Python3 program to implement
# the above appraoch
M = 1000000;
 
# Function to get the count
# of even length subarrays
# having bitwise xor 0
def cntSubXor(arr, N):
   
    # Stores prefix-xor of
    # the given array
    prefixXor = 0;
 
    # Stores prefix-xor at
    # even index of the array.
    Even =[0] * M;
 
    # Stores prefix-xor at
    # odd index of the array.
    Odd = [0] * M;
 
    # Stores count of subarrays
    # that satisfy the condition
    cntSub = 0;
 
    # length from 0 index
    # to odd index is even
    Odd[0] = 1;
 
    # Traverse the array.
    for i in range(0, N):
 
        # Take prefix-xor
        prefixXor ^= arr[i];
 
        # If index is odd
        if (i % 2 == 1):
 
            # Calculate pairs
            cntSub += Odd[prefixXor];
 
            # Increment prefix-xor
            # at odd index
            Odd[prefixXor] += 1;
        else:
 
            # Calculate pairs
            cntSub += Even[prefixXor];
 
            # Increment prefix-xor
            # at odd index
            Even[prefixXor] += 1;
 
    return cntSub;
 
# Driver Code
if __name__ == '__main__':
   
    arr = [2, 2, 3, 3,
           6, 7, 8];
    N = len(arr);
    print(cntSubXor(arr, N));
 
# This code is contributed by gauravrajput1

C#

// C# program to implement
// the above appraoch
using System;
 
class GFG{
     
static readonly int M =  1000000;
 
// Function to get the count
// of even length subarrays
// having bitwise xor 0
static int cntSubXor(int []arr, int N)
{
     
    // Stores prefix-xor of
    // the given array
    int prefixXor = 0;
 
    // Stores prefix-xor at
    // even index of the  array.
    int []Even = new int[M];
 
    // Stores prefix-xor at
    // odd index of the  array.
    int []Odd = new int[M];
 
    // Stores count of subarrays
    // that satisfy the condition
    int cntSub = 0;
 
    // length from 0 index
    // to odd index is even
    Odd[0] = 1;
 
    // Traverse the array.
    for(int i = 0; i < N; i++)
    {
         
        // Take prefix-xor
        prefixXor ^= arr[i];
 
        // If index is odd
        if (i % 2 == 1)
        {
             
            // Calculate pairs
            cntSub += Odd[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Odd[prefixXor]++;
        }
        else
        {
             
            // Calculate pairs
            cntSub += Even[prefixXor];
 
            // Increment prefix-xor
            // at odd index
            Even[prefixXor]++;
        }
    }
    return cntSub;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 2, 3, 3, 6, 7, 8 };
    int N = arr.Length;
     
    Console.Write(cntSubXor(arr, N));
}
}
 
// This code is contributed by gauravrajput1
输出
3

时间复杂度: O(N)
辅助空间: O(M),其中M是所有子阵列中可能的最大按位XOR。