Find the subarray of size K with minimum XOR
Last Updated :
12 Jul, 2024
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[] = {15, 10, 10, 12}, K = 2
Output: 0
Explanation:
The subarray {10, 10} 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++
// C++ implementation to find the
// subarray with minimum XOR
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// XOR of the subarray of size K
void findMinXORSubarray(int arr[],
int n, int k)
{
// K must be smaller
// than or equal to n
if (n < k)
return;
// Initialize beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for (int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for (int i = k; i < n; i++) {
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor) {
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
cout << min_xor << "\n";
}
// Driver Code
int main()
{
int arr[] = {15, 10, 10, 12} ;
int k = 2; // Subarray size
int n = sizeof arr / sizeof arr[0];
// Function Call
findMinXORSubarray(arr, n, k);
return 0;
}
Java
// Java implementation to find the
// subarray with minimum XOR
class GFG{
// Function to find the minimum
// XOR of the subarray of size K
static void findMinXORSubarray(int arr[],
int n, int k)
{
// K must be smaller
// than or equal to n
if (n < k)
return;
// Initialize beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for(int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for(int i = k; i < n; i++)
{
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
System.out.print(min_xor + "\n");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
// Subarray size
int k = 3;
int n = arr.length;
// Function Call
findMinXORSubarray(arr, n, k);
}
}
// This code is contributed by Amit Katiyar
Python
# Python3 implementation to find the
# subarray with minimum XOR
# Function to find the minimum
# XOR of the subarray of size K
def findMinXORSubarray(arr, n, k):
# K must be smaller
# than or equal to n
if (n < k):
return
# Initialize beginning
# index of result
res_index = 0
# Compute XOR sum of first
# subarray of size K
curr_xor = 0
for i in range(0, k):
curr_xor = curr_xor ^ arr[i]
# Initialize minimum XOR
# sum as current xor
min_xor = curr_xor
# Traverse from (k+1)'th
# element to n'th element
for i in range(k, n):
# XOR with current item
# and first item of
# previous subarray
curr_xor ^= (arr[i] ^ arr[i - k])
# Update result if needed
if (curr_xor < min_xor):
min_xor = curr_xor
res_index = (i - k + 1)
print(min_xor, end = '\n')
# Driver Code
arr = [ 3, 7, 90, 20, 10, 50, 40 ]
# Subarray size
k = 3
n = len(arr)
# Function Call
findMinXORSubarray(arr, n, k)
# This code is contributed by PratikBasu
C#
// C# implementation to find the
// subarray with minimum XOR
using System;
class GFG{
// Function to find the minimum
// XOR of the subarray of size K
static void findMinXORSubarray(int []arr,
int n, int k)
{
// K must be smaller
// than or equal to n
if (n < k)
return;
// Initialize beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for(int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for(int i = k; i < n; i++)
{
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
Console.Write(min_xor + "\n");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 7, 90, 20, 10, 50, 40 };
// Subarray size
int k = 3;
int n = arr.Length;
// Function Call
findMinXORSubarray(arr, n, k);
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript implementation to find the
// subarray with minimum XOR
// Function to find the minimum
// XOR of the subarray of size K
function findMinXORSubarray(arr, n, k)
{
// K must be smaller
// than or equal to n
if (n < k)
return;
// Initialize beginning
// index of result
let res_index = 0;
// Compute XOR sum of first
// subarray of size K
let curr_xor = 0;
for (let i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
let min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for (let i = k; i < n; i++) {
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor) {
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
document.write(min_xor + "<br>");
}
// Driver Code
let arr = [ 3, 7, 90, 20, 10, 50, 40 ];
let k = 3; // Subarray size
let n = arr.length;
// Function Call
findMinXORSubarray(arr, n, k);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Maximum XOR of k Size Subset Given an integer array arr[] of size n and an integer k. The task is to find the maximum XOR of the subset of size k from the given array arr[].Examples:Input: arr[] = [2, 5, 4, 1, 3, 7, 6, 8], k = 3Output: 15 Explanation: XOR of the elements of subset {2, 5, 8} is 15, which is the maximum possible.
14 min read
Find number of subarrays with XOR value a power of 2 Given an integer array, arr[] of size N. The XOR value of any subarray of arr[] is defined as the xor of all the integers in that subarray. The task is to find the number of sub-arrays with XOR value a power of 2. (1, 2, 4, 8, 16, ....)Examples: Input : arr[] = {2, 6, 7, 5, 8} Output : 6 Subarrays :
6 min read
Maximize the number of subarrays with XOR as zero Given an array of N numbers. The task is to maximize the number of subarrays with XOR value zero by swapping the bits of an array element of any given subarray any number of times. Note: 1<=A[i]<=1018 Examples: Input: a[] = {6, 7, 14} Output : 2 2 subarrays are {7, 14} and {6, 7 and 14} by swa
11 min read
Find the maximum subset XOR of a given set Given a set of positive integers. find the maximum XOR subset value in the given set. Expected time complexity O(n).Examples:Input: set[] = {2, 4, 5}Output: 7The subset {2, 5} has maximum XOR valueInput: set[] = {9, 8, 5}Output: 13The subset {8, 5} has maximum XOR valueInput: set[] = {8, 1, 2, 12, 7
15+ min read
Subarray with XOR less than k Given an array arr[] of n numbers and a number k. You have to write a program to find the number of subarrays with xor less than k. Examples:Â Input: arr[] = {8, 9, 10, 11, 12}, k=3Output: 3Explanation: There are 3 subarrays with XOR < 3: arr[0...1] = {8, 9} and 8 ^ 9 = 1arr[0...3] = {8, 9, 10, 1
15+ min read