Given an array arr[] and an integer K, the task is to count subarrays of size K in which every element appears an even number of times in the subarray.
Examples:
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
Naive Approach:
The idea is to generate all subarrays of size K and check each of them whether all its elements are present even a number of times or not.
Time complexity: O(N*K)
Efficient Approach:
The idea is to use Window Sliding and the XOR concept here.
- If the given K is odd, then return 0 as it is guaranteed that at least one number appears an odd number of times if K is odd.
- Check if K is greater than the length of arr[] then return 0.
- Initialize the following variables:
- count: Store the count of subarrays of size K with all elements.
- start: Remove left most element which is no longer part of k size subarray.
- currXor: Store Xor of the current subarray.
- Calculate the Xor of the first K size subarray and check if currXor becomes 0, then increment the count and update currXor by eliminating Xor with arr[start] and increment start by 1.
- Traverse arr[] from K to the length of arr[]:
- Update currXor by doing Xor with arr[i].
- Increment count if currXor becomes 0 otherwise ignore.
- Update currXor by eliminating Xor with arr[start].
- Increment start by 1.
- Return count.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countSubarray( int arr[], int K,
int N)
{
if (K % 2 != 0)
return 0;
if (N < K)
return 0;
int start = 0;
int i = 0;
int count = 0;
int currXor = arr[i++];
while (i < K)
{
currXor ^= arr[i];
i++;
}
if (currXor == 0)
count++;
currXor ^= arr[start++];
while (i < N)
{
currXor ^= arr[i];
i++;
if (currXor == 0)
count++;
currXor ^= arr[start++];
}
return count;
}
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));
}
|
Java
import java.util.*;
class GFG {
static int countSubarray( int [] arr,
int K, int N)
{
if (K % 2 != 0 )
return 0 ;
if (N < K)
return 0 ;
int start = 0 ;
int i = 0 ;
int count = 0 ;
int currXor = arr[i++];
while (i < K) {
currXor ^= arr[i];
i++;
}
if (currXor == 0 )
count++;
currXor ^= arr[start++];
while (i < N) {
currXor ^= arr[i];
i++;
if (currXor == 0 )
count++;
currXor ^= arr[start++];
}
return count;
}
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
def countSubarray(arr, K, N):
if (K % 2 ! = 0 ):
return 0
if (N < K):
return 0
start = 0
i = 0
count = 0
currXor = arr[i]
i + = 1
while (i < K):
currXor ^ = arr[i]
i + = 1
if (currXor = = 0 ):
count + = 1
currXor ^ = arr[start]
start + = 1
while (i < N):
currXor ^ = arr[i]
i + = 1
if (currXor = = 0 ):
count + = 1
currXor ^ = arr[start]
start + = 1
return count
if __name__ = = '__main__' :
arr = [ 2 , 4 , 4 , 2 , 2 , 4 ]
K = 4
N = len (arr)
print (countSubarray(arr, K, N))
|
C#
using System;
class GFG{
static int countSubarray( int [] arr,
int K, int N)
{
if (K % 2 != 0)
return 0;
if (N < K)
return 0;
int start = 0;
int i = 0;
int count = 0;
int currXor = arr[i++];
while (i < K)
{
currXor ^= arr[i];
i++;
}
if (currXor == 0)
count++;
currXor ^= arr[start++];
while (i < N)
{
currXor ^= arr[i];
i++;
if (currXor == 0)
count++;
currXor ^= arr[start++];
}
return count;
}
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));
}
}
|
Javascript
<script>
function countSubarray(arr, K, N)
{
if (K % 2 != 0)
return 0;
if (N < K)
return 0;
var start = 0;
var i = 0;
var count = 0;
var currXor = arr[i];
i++;
while (i < K)
{
currXor ^= arr[i];
i++;
}
if (currXor == 0)
count++;
currXor ^= arr[start];
start++;
while (i < N)
{
currXor ^= arr[i];
i++;
if (currXor == 0)
count++;
currXor ^= arr[start];
start++;
}
return count;
}
var arr = [2, 4, 4, 2, 2, 4];
var K = 4;
var N = arr.length;
document.write(countSubarray(arr, K, N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)