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