Find Maximum XOR value of a sub-array of size k
Last Updated :
18 Apr, 2023
Given an array of integers, the task is to find maximum XOR value of a subarray of size K.
Examples :
Input : arr[] = {2, 5, 8 ,1 , 1 ,3} k = 3
Output : 15
Explanation : All subarrays of size k (=3) and
their XOR values are:
{2, 5, 8} => XOR value = 15
{5, 8, 1} => XOR value = 12
{8, 1, 1} => XOR value = 8
{1, 1, 3} => XOR value = 3
Maximum of all XOR values = 15
Input : arr[] = {1, 2, 4, 5, 6}
Output : 6
A simple solution is to consider all subarrays of size k one by one and compute XOR value. Finally return maximum of all XOR values. This solution takes O(n*k) time.
An efficient solution takes O(n) time. The idea is simple, we can find XOR value of current subarray of size k by removing first element of previous subarray and adding last element of current subarray. We can remove an element from current XOR by doing XOR of it again because of property of XOR that a ^ x ^ x = a.
Algorithm :
Let input array be 'arr[]' and size of array be 'n'
max_xor ; // user to store maximum xor value
current_xor; // user to store xor value of current subarray
// of size k
// First compute xor value of first subarray of size k
// (i goes from 0 to k)
corrent_xor = current_xor ^ arr[i]
// Initialize maximum XOR
max_xor = current_xor
Traversal rest array (i goes from k to n-1 )
a). remove first element of previous subarray
current_xor = current_xor ^ arr[i-k]
b). add new element to subarray
current_xor = current_xor ^ arr[i]
c). update max_xor = max(max_xor, current_xor)
return max_xor
Below is the implementation of above steps.
C++
#include<iostream>
using namespace std;
int maximumXOR( int arr[] , int n , int k)
{
int current_xor = 0 ;
for ( int i = 0 ; i < k ; i++)
current_xor = current_xor ^ arr[i];
int max_xor = current_xor;
for ( int i = k ; i < n; i++)
{
current_xor = current_xor ^ arr[i-k];
current_xor = current_xor ^ arr[i];
max_xor = max(max_xor, current_xor);
}
return max_xor;
}
int main()
{
int arr[] = {2, 5, 8 ,1 , 1 ,3} ;
int k = 3;
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Maximum XOR : " << maximumXOR(arr, n, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maximumXOR( int arr[] , int n , int k)
{
int current_xor = 0 ;
for ( int i = 0 ; i < k ; i++)
current_xor = current_xor ^ arr[i];
int max_xor = current_xor;
for ( int i = k ; i < n; i++)
{
current_xor = current_xor ^ arr[i-k];
current_xor = current_xor ^ arr[i];
max_xor = Math.max(max_xor, current_xor);
}
return max_xor;
}
public static void main (String[] args)
{
int arr[] = { 2 , 5 , 8 , 1 , 1 , 3 } ;
int k = 3 ;
int n = arr.length;
System.out.println( "Maximum XOR : "
+ maximumXOR(arr, n, k));
}
}
|
Python 3
def maximumXOR(arr , n , k):
current_xor = 0
for i in range ( k):
current_xor = current_xor ^ arr[i]
max_xor = current_xor
for i in range ( k,n):
current_xor = current_xor ^ arr[i - k]
current_xor = current_xor ^ arr[i]
max_xor = max (max_xor, current_xor)
return max_xor
if __name__ = = "__main__" :
arr = [ 2 , 5 , 8 , 1 , 1 , 3 ]
k = 3
n = len (arr)
print ( "Maximum XOR : "
,maximumXOR(arr, n, k))
|
C#
using System;
class GFG {
static int maximumXOR( int []arr,
int n, int k)
{
int current_xor = 0 ;
for ( int i = 0; i < k; i++)
current_xor = current_xor ^ arr[i];
int max_xor = current_xor;
for ( int i = k ; i < n; i++)
{
current_xor = current_xor ^ arr[i-k];
current_xor = current_xor ^ arr[i];
max_xor = Math.Max(max_xor, current_xor);
}
return max_xor;
}
public static void Main ()
{
int []arr = {2, 5, 8 ,1 , 1 ,3} ;
int k = 3;
int n = arr.Length;
Console.WriteLine( "Maximum XOR : "
+ maximumXOR(arr, n, k));
}
}
|
PHP
<?php
function maximumXOR( $arr , $n , $k )
{
$current_xor = 0 ;
for ( $i = 0 ; $i < $k ; $i ++)
$current_xor = $current_xor ^
$arr [ $i ];
$max_xor = $current_xor ;
for ( $i = $k ; $i < $n ; $i ++)
{
$current_xor = $current_xor ^
$arr [ $i - $k ];
$current_xor = $current_xor ^
$arr [ $i ];
$max_xor = max( $max_xor ,
$current_xor );
}
return $max_xor ;
}
$arr = array (2, 5, 8, 1, 1, 3) ;
$k = 3;
$n = sizeof( $arr );
echo "Maximum XOR : " ,
maximumXOR( $arr , $n , $k );
?>
|
Javascript
<script>
function maximumXOR(arr,n,k)
{
let current_xor = 0 ;
for (let i = 0 ; i < k ; i++)
current_xor = current_xor ^ arr[i];
let max_xor = current_xor;
for (let i = k ; i < n; i++)
{
current_xor = current_xor ^ arr[i-k];
current_xor = current_xor ^ arr[i];
max_xor = Math.max(max_xor, current_xor);
}
return max_xor;
}
let arr=[2, 5, 8 ,1 , 1 ,3];
let k = 3;
let n = arr.length;
document.write( "Maximum XOR : "
+ maximumXOR(arr, n, k));
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...