给定数组arr []和整数K ,任务是计算大小为K的子数组,其中每个元素在子数组中出现偶数次。
例子:
Input: arr[] = {1, 4, 2, 10, 2, 10, 0, 20}, K = 4
Output: 1
Explanation: Only subarray {2, 10, 2, 10} satisfies the required condition.
Input: arr[] = {1, 4, 2, 10, 2, 3, 1, 0, 20}, K = 3
Output: 0
天真的方法:
这个想法是生成所有大小为K的子数组,并检查每个子数组中是否所有元素均偶数次出现。
时间复杂度: O(N * K)
高效方法:
这个想法是在这里使用“窗口滑动”和“异或”概念。
- 如果给定的K为奇数,则返回0,因为如果K为奇数,则确保至少一个数字出现奇数次。
- 检查K是否大于arr []的长度,然后返回0。
- 初始化以下变量:
- count:存储所有元素的大小为K的子数组的计数。
- start:删除不再是k大小子数组一部分的最左边的元素。
- currXor:存储当前子数组的Xor。
- 计算第一个K大小子数组的Xor ,并检查currXor是否为0 ,然后通过使用arr [start]消除Xor并递增start来增加计数并更新currXor 。
- 从K遍历arr []到arr []的长度:
- 通过对arr [i]进行Xor来更新currXor 。
- 如果currXor变为0,则增加计数,否则将忽略。
- 通过使用arr [start]消除Xor来更新currXor 。
- 增量从1开始。
- 返回计数。
下面是上述方法的实现:
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
Javascript
输出:
3
时间复杂度: O(N)
空间复杂度: O(1)