Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values
Given an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays).
Examples:
Input: arr[] = {1, 5, 7}, N = 3
Output: 7
Explanation:
The given array can be expressed as the 1 subarray i.e., {1, 5, 7}.
The Bitwise XOR of the Bitwise OR of the formed subarray is 7, which is the maximum possible value.
Input: arr[] = {1, 2}, N = 2
Output: 3
Naive Approach: The simplest approach to solve the given above problem is to generate all possible combinations of breaking of subarrays using recursion and at each recursive call, find the maximum value of Bitwise XOR of Bitwise OR of all possible formed subarray and print it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxXORUtil( int arr[], int N,
int xrr, int orr)
{
if (N == 0)
return xrr ^ orr;
int x = maxXORUtil(arr, N - 1,
xrr ^ orr,
arr[N - 1]);
int y = maxXORUtil(arr, N - 1,
xrr, orr | arr[N - 1]);
return max(x, y);
}
int maximumXOR( int arr[], int N)
{
return maxXORUtil(arr, N, 0, 0);
}
int main()
{
int arr[] = { 1, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumXOR(arr, N);
return 0;
}
|
Java
public class GFG{
static int maxXORUtil( int arr[], int N, int xrr,
int orr)
{
if (N == 0 )
return xrr ^ orr;
int x
= maxXORUtil(arr, N - 1 , xrr ^ orr, arr[N - 1 ]);
int y
= maxXORUtil(arr, N - 1 , xrr, orr | arr[N - 1 ]);
return Math.max(x, y);
}
static int maximumXOR( int arr[], int N)
{
return maxXORUtil(arr, N, 0 , 0 );
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 7 };
int N = arr.length;
System.out.println(maximumXOR(arr, N));
}
}
|
Python3
def maxXORUtil(arr, N, xrr, orr):
if (N = = 0 ):
return xrr ^ orr
x = maxXORUtil(arr, N - 1 , xrr ^ orr, arr[N - 1 ])
y = maxXORUtil(arr, N - 1 , xrr, orr | arr[N - 1 ])
return max (x, y)
def maximumXOR(arr, N):
return maxXORUtil(arr, N, 0 , 0 )
arr = 1 , 5 , 7
N = len (arr)
print (maximumXOR(arr, N))
|
C#
using System;
class GFG
{
static int maxXORUtil( int [] arr, int N, int xrr,
int orr)
{
if (N == 0)
return xrr ^ orr;
int x
= maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
int y
= maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
return Math.Max(x, y);
}
static int maximumXOR( int [] arr, int N)
{
return maxXORUtil(arr, N, 0, 0);
}
static void Main()
{
int [] arr = { 1, 5, 7 };
int N = arr.Length;
Console.Write(maximumXOR(arr, N));
}
}
|
Javascript
<script>
function maxXORUtil(arr,N,xrr,orr)
{
if (N == 0)
return xrr ^ orr;
let x
= maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
let y
= maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
return Math.max(x, y);
}
function maximumXOR(arr,N)
{
return maxXORUtil(arr, N, 0, 0);
}
let arr=[1, 5, 7 ];
let N = arr.length;
document.write(maximumXOR(arr, N));
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing the relationship between the Bitwise XOR and Bitwise OR i.e., the value of Bitwise XOR of N elements is at most the value of Bitwise OR of N elements. Therefore, to find the maximum value, the idea is to split the group into only 1 group of the whole array.
Hence, print the value of Bitwise OR of the array elements arr[] as the resultant maximum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxXOR( int arr[], int N)
{
int res = 0;
for ( int i = 0; i < N; i++) {
res |= arr[i];
}
return res;
}
int main()
{
int arr[] = { 1, 5, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << MaxXOR(arr, N);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int MaxXOR( int arr[], int N)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
res |= arr[i];
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 7 };
int N = arr.length;
System.out.println(MaxXOR(arr, N));
}
}
|
Python3
def MaxXOR(arr, N):
res = 0
for i in range (N):
res | = arr[i]
return res
if __name__ = = '__main__' :
arr = [ 1 , 5 , 7 ]
N = len (arr)
print (MaxXOR(arr, N))
|
C#
using System;
class GFG
{
static int MaxXOR( int []arr, int N)
{
int res = 0;
for ( int i = 0; i < N; i++)
{
res |= arr[i];
}
return res;
}
public static void Main(String[] args)
{
int []arr = { 1, 5, 7 };
int N = arr.Length;
Console.Write(MaxXOR(arr, N));
}
}
|
Javascript
<script>
function MaxXOR(arr, N)
{
var res = 0;
for ( var i = 0; i < N; i++)
{
res |= arr[i];
}
return res;
}
var arr = [ 1, 5, 7 ];
var N = arr.length;
document.write(MaxXOR(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...