📌  相关文章
📜  具有偶数频率的元素的大小为K的子数组的计数

📅  最后修改于: 2021-04-22 09:50:27             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是计算大小为K的子数组,其中每个元素在子数组中出现的次数均是偶数。

例子:

天真的方法:
这个想法是生成所有大小为K的子数组,并检查每个子数组中是否所有元素均偶数次出现。

时间复杂度: O(N * K)

高效方法:

想法是在这里使用“窗口滑动”和“异或”概念。

  1. 如果给定的K奇数,则返回0,因为如果K奇数,则保证至少有一个数字出现奇数次。
  2. 检查K是否大于arr []的长度,然后返回0。
  3. 初始化以下变量:
    • count:存储所有元素的大小为K的子数组的计数。
    • start:删除不再是k大小子数组一部分的最左边的元素。
    • currXor:存储当前子数组的Xor。
  4. 通过消除与异或计算ARR前K大小的子阵列的XOR,并检查是否currXor变为0,然后递增计数和更新currXor [开始]和由1个增量的开始
  5. K到arr []的长度遍历arr []:
    • 通过对arr [i]进行Xor来更新currXor
    • 如果currXor变为0,则增加计数,否则忽略。
    • 通过使用arr [start]消除Xor来更新currXor
    • 增量1开始
  6. 返回计数

下面是上述方法的实现:

C++
// C++ program to count subarrays
// of size K with all elements
// having even frequencies
#include
using namespace std;
  
// Function to return count of
// required subarrays
int countSubarray(int arr[], int K,
                             int N)
{
      
    // If K is odd
    if (K % 2 != 0)
          
        // Not possible to have
        // any such subarrays
        return 0;
  
    if (N < K)
        return 0;
  
    // Stores the starting index
    // of every subarrays
    int start = 0;
  
    int i = 0;
      
    // Stores the count of
    // required subarrays
    int count = 0;
      
    // Stores Xor of the
    // current subarray.
    int currXor = arr[i++];
  
    // Xor of first subarray
    // of size K
    while (i < K)
    {
        currXor ^= arr[i];
        i++;
    }
  
    // If all elements appear
    // evern number of times,
    // increase the count of
    // such subarrays
    if (currXor == 0)
        count++;
  
    // Remove the starting element
    // from the current subarray
    currXor ^= arr[start++];
  
    // Traverse the array
    // for the remaining
    // subarrays
    while (i < N) 
    {
          
        // Update Xor by adding the
        // last element of the
        // current subarray
        currXor ^= arr[i];
          
        // Increment i
        i++;
  
        // If currXor becomes 0,
        // then increment count
        if (currXor == 0)
            count++;
  
        // Update currXor by removing
        // the starting element of the
        // current subarray
        currXor ^= arr[start++];
    }
  
    // Return count
    return count;
}
  
// Driver Code
int main()
{
    int arr[] = { 2, 4, 4, 2, 2, 4 };
    int K = 4;
    int N = sizeof(arr) / sizeof(arr[0]);
      
    cout << (countSubarray(arr, K, N));
}
  
// This code is contributed by chitranayal


Java
// Java program to count subarrays
// of size K with all elements
// having even frequencies
  
import java.util.*;
  
class GFG {
  
    // Function to return count of
    // required subarrays
    static int countSubarray(int[] arr,
                             int K, int N)
    {
        // If K is odd
        if (K % 2 != 0)
            // Not possible to have
            // any such subarrays
            return 0;
  
        if (N < K)
            return 0;
  
        // Stores the starting index
        // of every subarrays
        int start = 0;
  
        int i = 0;
        // Stores the count of
        // required subarrays
        int count = 0;
        // Stores Xor of the
        // current subarray.
        int currXor = arr[i++];
  
        // Xor of first subarray
        // of size K
        while (i < K) {
            currXor ^= arr[i];
            i++;
        }
  
        // If all elements appear
        // evern number of times,
        // increase the count of
        // such subarrays
        if (currXor == 0)
            count++;
  
        // Remove the starting element
        // from the current subarray
        currXor ^= arr[start++];
  
        // Traverse the array
        // for the remaining
        // subarrays
        while (i < N) {
            // Update Xor by adding the
            // last element of the
            // current subarray
            currXor ^= arr[i];
            // Increment i
            i++;
  
            // If currXor becomes 0,
            // then increment count
            if (currXor == 0)
                count++;
  
            // Update currXor by removing
            // the starting element of the
            // current subarray
            currXor ^= arr[start++];
        }
  
        // Return count
        return count;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 2, 4, 4, 2, 2, 4 };
        int K = 4;
        int N = arr.length;
        System.out.println(
            countSubarray(arr, K, N));
    }
}


Python3
# Python3 program to count subarrays
# of size K with all elements
# having even frequencies
  
# Function to return count of
# required subarrays
def countSubarray(arr, K, N):
      
    # If K is odd
    if (K % 2 != 0):
          
        # Not possible to have
        # any such subarrays
        return 0
  
    if (N < K):
        return 0
  
    # Stores the starting index
    # of every subarrays
    start = 0
    i = 0
      
    # Stores the count of
    # required subarrays
    count = 0
      
    # Stores Xor of the
    # current subarray.
    currXor = arr[i]
    i += 1
  
    # Xor of first subarray
    # of size K
    while (i < K):
        currXor ^= arr[i]
        i += 1
  
    # If all elements appear
    # evern number of times,
    # increase the count of
    # such subarrays
    if (currXor == 0):
        count += 1
  
    # Remove the starting element
    # from the current subarray
    currXor ^= arr[start]
    start += 1
  
    # Traverse the array
    # for the remaining
    # subarrays
    while (i < N):
          
        # Update Xor by adding the
        # last element of the
        # current subarray
        currXor ^= arr[i]
          
        # Increment i
        i += 1
  
        # If currXor becomes 0,
        # then increment count
        if (currXor == 0):
            count += 1
  
        # Update currXor by removing
        # the starting element of the
        # current subarray
        currXor ^= arr[start]
        start += 1
  
    # Return count
    return count
      
# Driver Code
if __name__ == '__main__':
      
    arr = [ 2, 4, 4, 2, 2, 4 ]
    K = 4
    N = len(arr)
      
    print(countSubarray(arr, K, N))
  
# This code is contributed by mohit kumar 29


C#
// C# program to count subarrays
// of size K with all elements
// having even frequencies
using System;
class GFG{
  
// Function to return count of
// required subarrays
static int countSubarray(int[] arr,
                         int K, int N)
{
    // If K is odd
    if (K % 2 != 0)
      
        // Not possible to have
        // any such subarrays
        return 0;
  
    if (N < K)
        return 0;
  
    // Stores the starting index
    // of every subarrays
    int start = 0;
  
    int i = 0;
      
    // Stores the count of
    // required subarrays
    int count = 0;
      
    // Stores Xor of the
    // current subarray.
    int currXor = arr[i++];
  
    // Xor of first subarray
    // of size K
    while (i < K)
    {
        currXor ^= arr[i];
        i++;
    }
  
    // If all elements appear
    // evern number of times,
    // increase the count of
    // such subarrays
    if (currXor == 0)
        count++;
  
    // Remove the starting element
    // from the current subarray
    currXor ^= arr[start++];
  
    // Traverse the array
    // for the remaining
    // subarrays
    while (i < N)
    {
        // Update Xor by adding the
        // last element of the
        // current subarray
        currXor ^= arr[i];
          
        // Increment i
        i++;
  
        // If currXor becomes 0,
        // then increment count
        if (currXor == 0)
            count++;
  
        // Update currXor by removing
        // the starting element of the
        // current subarray
        currXor ^= arr[start++];
    }
  
    // Return count
    return count;
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 2, 4, 4, 2, 2, 4 };
    int K = 4;
    int N = arr.Length;
    Console.Write(countSubarray(arr, K, N));
}
}
  
// This code is contributed by Akanksha_Rai


输出:
3

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