Find a K-length subarray having Bitwise XOR equal to that of remaining array elements
Last Updated :
18 Apr, 2023
Given an array arr[] of size N, the task is to check if any subarray of size K exists in the array or not, whose Bitwise XOR is equal to the Bitwise XOR of the remaining array elements. If found to be true, then print “YES”. Otherwise, print “NO”.
Examples:
Input: arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 }, K = 5
Output: YES
Explanation:
Bitwise XOR of the subarray { 3, 3, 5, 7, 7 } is equal to 5
Bitwise XOR of { 2, 3, 4 } is equal to 5.
Therefore, the required output is YES.
Input: arr[] = { 2, 3, 4, 5, 6, 7, 4 }, K = 2
Output: NO
Naive Approach: The simplest approach to solve this problem is to generate all subarrays of size K. For each subarray, check if the bitwise XOR of the subarray is equal to bitwise XOR of remaining elements or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Sliding Window Technique Following are the observations:
If X ^ Y = Z, then X ^ Z = Y
SubarrayXOR = arr[i] ^ arr[i + 1] ^ … ^ arr[j]
totalXOR = arr[0] ^ arr[1] ^ arr[2] ….. ^ arr[N – 1]
Bitwise XOR of the remaining array elements = totalXOR ^ SubarrayXOR
- Calculate the Bitwise XOR of all array elements, say totalXOR.
- Calculate the Bitwise XOR of first K elements of the array, say SubarrayXOR.
- Use sliding window technique, traverse each subarray of size K and check if Bitwise XOR of the subarray is equal to the Bitwise XOR of the remaining array elements or not. If found to be true, then print “YES”.
- Otherwise, print “NO”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSubarrayExistUtil( int arr[], int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
for ( int i = 0; i < N; i++)
totalXOR ^= arr[i];
for ( int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
for ( int i = K; i < N; i++) {
SubarrayXOR ^= arr[i];
SubarrayXOR ^= arr[i - 1];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
}
return false ;
}
void isSubarrayExist( int arr[], int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
cout << "YES\n" ;
else
cout << "NO\n" ;
}
int32_t main()
{
int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
isSubarrayExist(arr, K, N);
}
|
C
#include <stdbool.h> //to use true, false keywords
#include <stdint.h> //to use int_32
#include <stdio.h>
bool isSubarrayExistUtil( int arr[], int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
for ( int i = 0; i < N; i++)
totalXOR ^= arr[i];
for ( int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
for ( int i = K; i < N; i++) {
SubarrayXOR ^= arr[i];
SubarrayXOR ^= arr[i - 1];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
}
return false ;
}
void isSubarrayExist( int arr[], int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
printf ( "YES\n" );
else
printf ( "NO\n" );
}
int32_t main()
{
int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
isSubarrayExist(arr, K, N);
}
|
Java
import java.util.*;
class GFG{
static boolean isSubarrayExistUtil( int arr[],
int K, int N)
{
int totalXOR = 0 ;
int SubarrayXOR = 0 ;
for ( int i = 0 ; i < N; i++)
totalXOR ^= arr[i];
for ( int i = 0 ; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
for ( int i = K; i < N; i++)
{
SubarrayXOR ^= arr[i];
SubarrayXOR ^= arr[i - 1 ];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
}
return false ;
}
static void isSubarrayExist( int arr[],
int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
System.out.print( "YES\n" );
else
System.out.print( "NO\n" );
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 };
int N = arr.length;
int K = 5 ;
isSubarrayExist(arr, K, N);
}
}
|
Python3
def isSubarrayExistUtil(arr, K, N):
totalXOR = 0
SubarrayXOR = 0
for i in range (N):
totalXOR ^ = arr[i]
for i in range (K):
SubarrayXOR ^ = arr[i]
if (SubarrayXOR = = (totalXOR ^ SubarrayXOR)):
return True
for i in range (K, N):
SubarrayXOR ^ = arr[i]
SubarrayXOR ^ = arr[i - 1 ]
if (SubarrayXOR = = (totalXOR ^ SubarrayXOR)):
return True
return False
def isSubarrayExist(arr, K, N):
if (isSubarrayExistUtil(arr, K, N)):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = '__main__' :
arr = [ 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 ]
N = len (arr)
K = 5
isSubarrayExist(arr, K, N)
|
C#
using System;
class GFG
{
static bool isSubarrayExistUtil( int []arr,
int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
for ( int i = 0; i < N; i++)
totalXOR ^= arr[i];
for ( int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
for ( int i = K; i < N; i++)
{
SubarrayXOR ^= arr[i];
SubarrayXOR ^= arr[i - 1];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
}
return false ;
}
static void isSubarrayExist( int []arr,
int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
Console.Write( "YES\n" );
else
Console.Write( "NO\n" );
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 3, 5, 7, 7, 3, 4 };
int N = arr.Length;
int K = 5;
isSubarrayExist(arr, K, N);
}
}
|
Javascript
<script>
function isSubarrayExistUtil(arr , K , N)
{
var totalXOR = 0;
var SubarrayXOR = 0;
for (i = 0; i < N; i++)
totalXOR ^= arr[i];
for (i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true ;
for (i = K; i < N; i++) {
SubarrayXOR ^= arr[i];
SubarrayXOR ^= arr[i - 1];
if (SubarrayXOR ==
(totalXOR ^ SubarrayXOR))
return true ;
}
return false ;
}
function isSubarrayExist(arr , K , N) {
if (isSubarrayExistUtil(arr, K, N))
document.write( "YES\n" );
else
document.write( "NO\n" );
}
var arr = [ 2, 3, 3, 5, 7, 7, 3, 4 ];
var N = arr.length;
var K = 5;
isSubarrayExist(arr, K, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...