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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
bool isSubarrayExistUtil(int arr[], int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
// Find XOR of whole array
for (int i = 0; i < N; i++)
totalXOR ^= arr[i];
// Find XOR of first K elements
for (int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
for (int i = K; i < N; i++) {
// Adding XOR of next element
SubarrayXOR ^= arr[i];
// Removing XOR of previous element
SubarrayXOR ^= arr[i - 1];
// Check if XOR of current subarray matches
// with the XOR of remaining elements or not
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
}
return false;
}
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
void isSubarrayExist(int arr[], int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
cout << "YES\n";
else
cout << "NO\n";
}
// Driver Code
int32_t main()
{
// Given array
int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given K
int K = 5;
// Function Call
isSubarrayExist(arr, K, N);
}
C
// C program to implement
// the above approach
#include <stdbool.h> //to use true, false keywords
#include <stdint.h> //to use int_32
#include <stdio.h>
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
bool isSubarrayExistUtil(int arr[], int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
// Find XOR of whole array
for (int i = 0; i < N; i++)
totalXOR ^= arr[i];
// Find XOR of first K elements
for (int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
for (int i = K; i < N; i++) {
// Adding XOR of next element
SubarrayXOR ^= arr[i];
// Removing XOR of previous element
SubarrayXOR ^= arr[i - 1];
// Check if XOR of current subarray matches
// with the XOR of remaining elements or not
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
}
return false;
}
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
void isSubarrayExist(int arr[], int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
printf("YES\n");
else
printf("NO\n");
}
// Driver Code
int32_t main()
{
// Given array
int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Given K
int K = 5;
// Function Call
isSubarrayExist(arr, K, N);
}
// This code is contributed by phalashi.
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
static boolean isSubarrayExistUtil(int arr[],
int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
// Find XOR of whole array
for(int i = 0; i < N; i++)
totalXOR ^= arr[i];
// Find XOR of first K elements
for(int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
for(int i = K; i < N; i++)
{
// Adding XOR of next element
SubarrayXOR ^= arr[i];
// Removing XOR of previous element
SubarrayXOR ^= arr[i - 1];
// Check if XOR of current subarray matches
// with the XOR of remaining elements or not
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
}
return false;
}
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
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");
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 2, 3, 3, 5, 7, 7, 3, 4 };
// Size of the array
int N = arr.length;
// Given K
int K = 5;
// Function Call
isSubarrayExist(arr, K, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Utility function to check if subarray
# of size K exits whose XOR of elements
# equal to XOR ofremaning array elements
def isSubarrayExistUtil(arr, K, N):
totalXOR = 0
SubarrayXOR = 0
# Find XOR of whole array
for i in range(N):
totalXOR ^= arr[i]
# Find XOR of first K elements
for i in range(K):
SubarrayXOR ^= arr[i]
if (SubarrayXOR == (totalXOR ^ SubarrayXOR)):
return True
for i in range(K, N):
# Adding XOR of next element
SubarrayXOR ^= arr[i]
# Removing XOR of previous element
SubarrayXOR ^= arr[i - 1]
# Check if XOR of current subarray matches
# with the XOR of remaining elements or not
if (SubarrayXOR == (totalXOR ^ SubarrayXOR)):
return True
return False
# Function to check if subarray of size
# K exits whose XOR of elements equal
# to XOR ofremaning array elements
def isSubarrayExist(arr, K, N):
if (isSubarrayExistUtil(arr, K, N)):
print("YES")
else:
print("NO")
# Driver Code
if __name__ == '__main__':
# Given array
arr = [2, 3, 3, 5, 7, 7, 3, 4]
# Size of the array
N = len(arr)
# Given K
K = 5
# Function Call
isSubarrayExist(arr, K, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
static bool isSubarrayExistUtil(int []arr,
int K, int N)
{
int totalXOR = 0;
int SubarrayXOR = 0;
// Find XOR of whole array
for(int i = 0; i < N; i++)
totalXOR ^= arr[i];
// Find XOR of first K elements
for(int i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
for(int i = K; i < N; i++)
{
// Adding XOR of next element
SubarrayXOR ^= arr[i];
// Removing XOR of previous element
SubarrayXOR ^= arr[i - 1];
// Check if XOR of current subarray matches
// with the XOR of remaining elements or not
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
}
return false;
}
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
static void isSubarrayExist(int []arr,
int K, int N)
{
if (isSubarrayExistUtil(arr, K, N))
Console.Write("YES\n");
else
Console.Write("NO\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 2, 3, 3, 5, 7, 7, 3, 4 };
// Size of the array
int N = arr.Length;
// Given K
int K = 5;
// Function Call
isSubarrayExist(arr, K, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program to implement
// the above approach
// Utility function to check if subarray
// of size K exits whose XOR of elements
// equal to XOR ofremaning array elements
function isSubarrayExistUtil(arr , K , N)
{
var totalXOR = 0;
var SubarrayXOR = 0;
// Find XOR of whole array
for (i = 0; i < N; i++)
totalXOR ^= arr[i];
// Find XOR of first K elements
for (i = 0; i < K; i++)
SubarrayXOR ^= arr[i];
if (SubarrayXOR == (totalXOR ^ SubarrayXOR))
return true;
for (i = K; i < N; i++) {
// Adding XOR of next element
SubarrayXOR ^= arr[i];
// Removing XOR of previous element
SubarrayXOR ^= arr[i - 1];
// Check if XOR of current
// subarray matches
// with the XOR of remaining
// elements or not
if (SubarrayXOR ==
(totalXOR ^ SubarrayXOR))
return true;
}
return false;
}
// Function to check if subarray of size
// K exits whose XOR of elements equal
// to XOR ofremaning array elements
function isSubarrayExist(arr , K , N) {
if (isSubarrayExistUtil(arr, K, N))
document.write("YES\n");
else
document.write("NO\n");
}
// Driver Code
// Given array
var arr = [ 2, 3, 3, 5, 7, 7, 3, 4 ];
// Size of the array
var N = arr.length;
// Given K
var K = 5;
// Function Call
isSubarrayExist(arr, K, N);
// This code contributed by Rajput-Ji
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count even length subarrays having bitwise XOR equal to 0 Given an array arr[] of size N, the task is to count all possible even length subarrays having bitwise XOR of subarray elements equal to 0. Examples: Input: arr[] = {2, 2, 3, 3, 6, 7, 8}Output: 3Explanation:Subarrays having XOR of elements equal to 0 are: {{2, 2}, {3, 3}, {2, 2, 3, 3}}Therefore, the
13 min read
Minimum Bitwise XOR operations to make any two array elements equal Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make a
9 min read
Count ways to split array into three non-empty subarrays having equal Bitwise XOR values Given an array arr[] consisting of N non-negative integers, the task is to count the number of ways to split the array into three different non-empty subarrays such that Bitwise XOR of each subarray is equal. Examples: Input: arr[] = {7, 0, 5, 2, 7} Output: 2Explanation: All possible ways are:{{7},
9 min read
Count N-length arrays of made up of elements not exceeding 2^K - 1 having maximum sum and Bitwise AND equal to 0 Given two integers N and K, the task is to find the number of N-length arrays that satisfies the following conditions: The sum of the array elements is maximum possible.For every possible value of i ( 1 ? i ? N ), the ith element should lie between 0 and 2K - 1.Also, Bitwise AND of all the array ele
7 min read
Minimize flips on K-length subarrays required to make all array elements equal to 1 Given a binary array arr[] of size N and a positive integer K, the task is to find the minimum number of times any subarray of size K from the given array arr[] is required to be flipped to make all array elements equal to 1. If it is not possible to do so, then print "-1". Examples: Input: arr[] =
15+ min read
Minimum Bitwise OR operations to make any two array elements equal Given an array arr[] of integers and an integer K, we can perform the Bitwise OR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two eleme
9 min read
Create an array such that XOR of subarrays of length K is X Given three integers N, K and X, the task is to construct an array of length N, in which XOR of all elements of each contiguous sub-array of length K is X.Examples: Input: N = 5, K = 1, X = 4 Output: 4 4 4 4 4 Explanation: Each subarray of length 1 has Xor value equal to 4.Input: N = 5, K = 2, X = 4
4 min read
XOR of all elements of array with set bits equal to K Given an array of integers and a number K. The task is to find the XOR of only those elements of the array whose total set bits are equal to K. Examples: Input : arr[] = {1, 22, 3, 10}, K=1 Output : 1 Elements with set bits equal to 1 is 1. So, XOR is also 1. Input : arr[] = {3, 4, 10, 5, 8}, K=2 Ou
4 min read
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read
Find an N-length permutation that contains subarrays with sum less than Bitwise XOR Given a positive integer N, the task is to find a permutation of length N having Bitwise OR of any of its subarray greater than or equal to the length of the subarray. Examples: Input: N = 5 Output: 1 3 5 2 4 Explanation: Consider the subarray {1, 3, 5} from the permutation {1, 3, 5, 2, $}. Length o
3 min read