Skip to content
Related Articles

Related Articles

Improve Article

Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values

  • Last Updated : 08 Jul, 2021

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to find all the
// possible breaking of arrays into
// subarrays and find the maximum
// Bitwise XOR
int maxXORUtil(int arr[], int N,
               int xrr, int orr)
{
    // If the value of N is 0
    if (N == 0)
        return xrr ^ orr;
 
    // Stores the result if the new
    // group is formed with the first
    // element as arr[i]
    int x = maxXORUtil(arr, N - 1,
                       xrr ^ orr,
                       arr[N - 1]);
 
    // Stores if the result if the
    // arr[i] is included in the
    // last group
    int y = maxXORUtil(arr, N - 1,
                       xrr, orr | arr[N - 1]);
 
    // Returns the maximum of
    // x and y
    return max(x, y);
}
 
// Function to find the maximum possible
// Bitwise XOR of all possible values of
// the array after breaking the arrays
// into subarrays
int maximumXOR(int arr[], int N)
{
    // Return the result
    return maxXORUtil(arr, N, 0, 0);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << maximumXOR(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
public class GFG{
 
    // Recursive function to find all the
    // possible breaking of arrays into
    // subarrays and find the maximum
    // Bitwise XOR
    static int maxXORUtil(int arr[], int N, int xrr,
                          int orr)
    {
       
        // If the value of N is 0
        if (N == 0)
            return xrr ^ orr;
 
        // Stores the result if the new
        // group is formed with the first
        // element as arr[i]
        int x
            = maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
 
        // Stores if the result if the
        // arr[i] is included in the
        // last group
        int y
            = maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
 
        // Returns the maximum of
        // x and y
        return Math.max(x, y);
    }
 
    // Function to find the maximum possible
    // Bitwise XOR of all possible values of
    // the array after breaking the arrays
    // into subarrays
    static int maximumXOR(int arr[], int N)
    {
       
        // Return the result
        return maxXORUtil(arr, N, 0, 0);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 5, 7 };
        int N = arr.length;
        System.out.println(maximumXOR(arr, N));
    }
}
 
// This code is contributed by abhinavjain194

Python3




# C++ program for the above approach
# Recursive function to find all the
# possible breaking of arrays o
# subarrays and find the maximum
# Bitwise XOR
def maxXORUtil(arr, N, xrr, orr):
 
    # If the value of N is 0
    if (N == 0):
        return xrr ^ orr
 
    # Stores the result if the new
    # group is formed with the first
    # element as arr[i]
    x = maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1])
 
    # Stores if the result if the
    # arr[i] is included in the
    # last group
    y = maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1])
 
    # Returns the maximum of
    # x and y
    return max(x, y)
 
 
# Function to find the maximum possible
# Bitwise XOR of all possible values of
# the array after breaking the arrays
# o subarrays
def maximumXOR(arr,  N):
 
    # Return the result
    return maxXORUtil(arr, N, 0, 0)
 
 
# Driver Code
arr =  1, 5, 7
N = len(arr)
print(maximumXOR(arr, N))
 
# this code is contributed by shivanisinghss2110

C#




// C# program for the above approach
using System;
class GFG
{
 
    // Recursive function to find all the
    // possible breaking of arrays into
    // subarrays and find the maximum
    // Bitwise XOR
    static int maxXORUtil(int[] arr, int N, int xrr,
                          int orr)
    {
       
        // If the value of N is 0
        if (N == 0)
            return xrr ^ orr;
 
        // Stores the result if the new
        // group is formed with the first
        // element as arr[i]
        int x
            = maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
 
        // Stores if the result if the
        // arr[i] is included in the
        // last group
        int y
            = maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
 
        // Returns the maximum of
        // x and y
        return Math.Max(x, y);
    }
 
    // Function to find the maximum possible
    // Bitwise XOR of all possible values of
    // the array after breaking the arrays
    // into subarrays
    static int maximumXOR(int[] arr, int N)
    {
       
        // Return the result
        return maxXORUtil(arr, N, 0, 0);
    }
 
 
// Driver code
static void Main()
{
    int[] arr = { 1, 5, 7 };
        int N = arr.Length;
        Console.Write(maximumXOR(arr, N));
}
}
 
// This code is contributed by sanjoy_62.

Javascript




<script>
// Javascript program for the above approach
 
// Recursive function to find all the
// possible breaking of arrays into
// subarrays and find the maximum
// Bitwise XOR
function maxXORUtil(arr,N,xrr,orr)
{
 
    // If the value of N is 0
        if (N == 0)
            return xrr ^ orr;
  
        // Stores the result if the new
        // group is formed with the first
        // element as arr[i]
        let x
            = maxXORUtil(arr, N - 1, xrr ^ orr, arr[N - 1]);
  
        // Stores if the result if the
        // arr[i] is included in the
        // last group
        let y
            = maxXORUtil(arr, N - 1, xrr, orr | arr[N - 1]);
  
        // Returns the maximum of
        // x and y
        return Math.max(x, y);
}
 
// Function to find the maximum possible
// Bitwise XOR of all possible values of
// the array after breaking the arrays
// into subarrays
function maximumXOR(arr,N)
{
 
    // Return the result
    return maxXORUtil(arr, N, 0, 0);
}
 
// Driver code
let arr=[1, 5, 7 ];
let N = arr.length;
document.write(maximumXOR(arr, N));
 
// This code is contributed by unknown2108
</script>
Output: 
7

 

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the bitwise OR of
// array elements
int MaxXOR(int arr[], int N)
{
    // Stores the resultant maximum
    // value of Bitwise XOR
    int res = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        res |= arr[i];
    }
 
    // Return the maximum value res
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << MaxXOR(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
     
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int arr[], int N)
{
     
    // Stores the resultant maximum
    // value of Bitwise XOR
    int res = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
        res |= arr[i];
    }
     
    // Return the maximum value res
    return res;
}
 
public static void main(String[] args)
{
    int arr[] = { 1, 5, 7 };
    int N = arr.length;
     
    System.out.println(MaxXOR(arr, N));
}
}
 
// This code is contributed by offbeat

Python3




# Python3 program for the above approach
 
# Function to find the bitwise OR of
# array elements
def MaxXOR(arr, N):
     
    # Stores the resultant maximum
    # value of Bitwise XOR
    res = 0
 
    # Traverse the array arr[]
    for i in range(N):
        res |= arr[i]
 
    # Return the maximum value res
    return res
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 5, 7 ]
    N = len(arr)
     
    print (MaxXOR(arr, N))
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
 
class GFG
{
     
// Function to find the bitwise OR of
// array elements
static int MaxXOR(int []arr, int N)
{
     
    // Stores the resultant maximum
    // value of Bitwise XOR
    int res = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
        res |= arr[i];
    }
     
    // Return the maximum value res
    return res;
}
 
public static void Main(String[] args)
{
    int []arr = { 1, 5, 7 };
    int N = arr.Length;
     
    Console.Write(MaxXOR(arr, N));
}
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to find the bitwise OR of
// array elements
function MaxXOR(arr, N)
{
     
    // Stores the resultant maximum
    // value of Bitwise XOR
    var res = 0;
 
    // Traverse the array arr[]
    for(var i = 0; i < N; i++)
    {
        res |= arr[i];
    }
     
    // Return the maximum value res
    return res;
}
 
// Driver code
var arr = [ 1, 5, 7 ];
var N = arr.length;
 
document.write(MaxXOR(arr, N));
 
// This code is contributed by shivanisinghss2110
 
</script>
Output: 
7

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :