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

📅  最后修改于: 2021-09-06 06:23:56             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是计算所有可能的偶数长度子数组,其子数组元素的按位异或等于0

例子:

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

  • 初始化一个变量,比如res来存储满足给定条件的子数组的数量。
  • 遍历数组并生成所有可能的子数组。
  • 对于每个子数组,检查子数组的长度是否为偶数且其元素的按位异或为 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


Javascript


输出
3

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

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

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

  • 初始化两个数组,比如Even[]Odd[]以分别存储给定数组的偶数和奇数索引处的 Prefix XOR 频率。
  • 初始化一个变量,比如cntSub ,以存储满足给定条件的子数组的计数。
  • 遍历给定数组并计算给定数组的前缀异或。
  • Prefix 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

蟒蛇3

# 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

Javascript


输出
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live