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

📅  最后修改于: 2021-09-02 07:39:05             🧑  作者: Mango

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

例子:

天真的方法:
这个想法是生成大小为K 的所有子数组,并检查每个子数组是否所有元素都存在甚至多次。

时间复杂度: O(N*K)
有效的方法:

这个想法是在这里使用Window SlidingXOR概念。

  1. 如果给定的K奇数,则返回 0,因为如果K奇数,则保证至少有一个数字出现奇数次。
  2. 检查 K 是否大于 arr[] 的长度,然后返回 0。
  3. 初始化以下变量:
    • count:存储大小为K的所有元素的子数组的计数。
    • 开始:删除不再是 k 大小子数组的一部分的最左边的元素。
    • currXor:存储当前子数组的异或。
  4. 计算第一个 K 大小的子数组Xor并检查currXor 是否变为0 ,然后增加计数并通过使用arr[start]消除Xor并增加start 1 来更新currXor
  5. 将 arr[] 从K遍历到 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
    // even 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
        // even 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
    # even 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
    // even 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


Javascript


输出:
3

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

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