Find the subarray of size K with minimum XOR
Given an array arr[] and integer K, the task is to find the minimum bitwise XOR sum of any subarray of size K in the given array.
Examples:
Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, K = 3
Output: 16
Explanation:
The subarray {10, 50, 40} has the minimum XOR
Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, K = 2
Output: 17
Explanation:
The subarray {5, 20} has the minimum XOR
Naive Approach: A Simple Solution is to consider every element as the beginning of subarray of size k and compute XOR of subarray starting with this element.
Time complexity: O(N * K)
Efficient Approach: The idea is to use the sliding window technique of size K and keep track of XOR sum of current K elements. To compute the XOR of the current window, perform XOR with the first element of the previous window to discard that element and with the current element to add this element into the window. Similarly, slide the windows to find the minimum XOR of the subarray of size K.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinXORSubarray( int arr[],
int n, int k)
{
if (n < k)
return ;
int res_index = 0;
int curr_xor = 0;
for ( int i = 0; i < k; i++)
curr_xor ^= arr[i];
int min_xor = curr_xor;
for ( int i = k; i < n; i++) {
curr_xor ^= (arr[i] ^ arr[i - k]);
if (curr_xor < min_xor) {
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
cout << min_xor << "\n" ;
}
int main()
{
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
int k = 3;
int n = sizeof arr / sizeof arr[0];
findMinXORSubarray(arr, n, k);
return 0;
}
|
Java
class GFG{
static void findMinXORSubarray( int arr[],
int n, int k)
{
if (n < k)
return ;
int res_index = 0 ;
int curr_xor = 0 ;
for ( int i = 0 ; i < k; i++)
curr_xor ^= arr[i];
int min_xor = curr_xor;
for ( int i = k; i < n; i++)
{
curr_xor ^= (arr[i] ^ arr[i - k]);
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1 );
}
}
System.out.print(min_xor + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 };
int k = 3 ;
int n = arr.length;
findMinXORSubarray(arr, n, k);
}
}
|
Python3
def findMinXORSubarray(arr, n, k):
if (n < k):
return
res_index = 0
curr_xor = 0
for i in range ( 0 , k):
curr_xor = curr_xor ^ arr[i]
min_xor = curr_xor
for i in range (k, n):
curr_xor ^ = (arr[i] ^ arr[i - k])
if (curr_xor < min_xor):
min_xor = curr_xor
res_index = (i - k + 1 )
print (min_xor, end = '\n' )
arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ]
k = 3
n = len (arr)
findMinXORSubarray(arr, n, k)
|
C#
using System;
class GFG{
static void findMinXORSubarray( int []arr,
int n, int k)
{
if (n < k)
return ;
int res_index = 0;
int curr_xor = 0;
for ( int i = 0; i < k; i++)
curr_xor ^= arr[i];
int min_xor = curr_xor;
for ( int i = k; i < n; i++)
{
curr_xor ^= (arr[i] ^ arr[i - k]);
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
Console.Write(min_xor + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 3, 7, 90, 20, 10, 50, 40 };
int k = 3;
int n = arr.Length;
findMinXORSubarray(arr, n, k);
}
}
|
Javascript
<script>
function findMinXORSubarray(arr, n, k)
{
if (n < k)
return ;
let res_index = 0;
let curr_xor = 0;
for (let i = 0; i < k; i++)
curr_xor ^= arr[i];
let min_xor = curr_xor;
for (let i = k; i < n; i++) {
curr_xor ^= (arr[i] ^ arr[i - k]);
if (curr_xor < min_xor) {
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
document.write(min_xor + "<br>" );
}
let arr = [ 3, 7, 90, 20, 10, 50, 40 ];
let k = 3;
let n = arr.length;
findMinXORSubarray(arr, n, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...