Maximum XOR subarray | Practice | GeeksforGeeks

Given an array arr[] of size, N. Find the subarray with maximum XOR. A subarray is a contiguous part of the array.

Example 1:
Input:
N = 4
arr[] = {1,2,3,4}
Output: 7
Explanation: 
The subarray {3,4} has maximum xor 
value equal to 7.


Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxSubarrayXOR() which takes the array arr[], its size N as input parameters and returns the maximum xor of any subarray.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

 
Constraints:
1 <= N <= 105
1 <= arr[i] <= 105
0
apoorvmishra2 months ago
struct node{   node* child[2];   int value;   node(){       child[0]=NULL;       child[1]=NULL;       value=0;   }};class trie{   public:   node* root;   trie(){       root= new node;   }   void insert(int num){       node* temp=root;       for(int i=31;i>=0;i--){           bool bit=num&(1<<i);           if(temp->child[bit]==NULL){               temp->child[bit]=new node;           }           temp=temp->child[bit];       }       temp->value=num;   }   int query(int num){       node* temp=root;       for(int i=31;i>=0;i--){           bool bit=num&(1<<i);           if(temp->child[1-bit]!=NULL){               temp=temp->child[1-bit];           }else if(temp->child[bit]!=NULL){               temp=temp->child[bit];           }       }       return (temp->value)^num;   }};class Solution{   public:   int maxSubarrayXOR(int N, int arr[]){           trie t;       t.insert(0);       int res=0,prefixXor=0;       for(int i=0;i<N;i++){           prefixXor=prefixXor^arr[i];           t.insert(prefixXor);           res=max(res,t.query(prefixXor));       }       return res;   }};
0
mayank20213 months ago
@gfg please see.. backend code seems wrong in this case.for this testcase, it's showing 100001 as output. I tried manually but i can't get above 10000. what am I missing here ?3 100000 99999 99998
-1
aloksinghbais023 months ago
C++ solution using kadane's algorithm having time complexity as O(N) and space complexity as O(1) is as follows :-
 
Execution Time :- 0.1 / 1.2 sec
 
int maxSubarrayXOR(int N, int arr[]){           if(N==1) return (arr[0]);       int max_so_far = 0;       int curr_max = 1;       for(int i=0; i<N; i++){           curr_max = max(arr[i],arr[i] ^ curr_max);           max_so_far = max(max_so_far,curr_max);       }       return (max_so_far);   }
-1
devshukla07
This comment was deleted.
-9
amail4muni4 months ago
for java solution
 
class Solution{
   static int maxSubarrayXOR(int N, int arr[]){       // code here         int ans = 1; // Initialize result       int curr_xor = 1;        // Pick starting points of subarrays        for (int i=0; i<N; i++)        {               curr_xor = Math.max(arr[i],curr_xor^arr[i]);                ans = Math.max(ans, curr_xor);        }        return ans;    }}
-11
sreenathmopuri4 months ago
class Solution{   public:     int maxSubarrayXOR(int N, int arr[]){              //code here          int max_so_far = 1, maxXor = 1;          for (int i = 0; i < N; i++)          {               max_so_far = max(arr[i], max_so_far ^ arr[i]);               maxXor = max(maxXor, max_so_far);         }         return maxXor;     }};
0
dmaddalena694 months ago
This is the solution but I cannot decrease the execution time of the program. Anyone can help me?
int maxSubarrayXOR(int N, int arr[]){    
        
        int max_xor = 0, ele_subarr = 1, re_xor = 0;
        
        while(ele_subarr <= N)
        {
            for(int i = 0; i < N-(ele_subarr-1); i++)
            {
                for(int j = i; j < ele_subarr+i; j++)
                    re_xor ^= arr[j];
                
                if(re_xor > max_xor)
                    max_xor = re_xor;
                    
                re_xor = 0;
            }
            
            ele_subarr++;
        }
            
        return max_xor;
    }
0
tanmaymm585 months ago
Why isn't this solution working? I am gettig the output 2 and the XOR of 4 and 6 is 2 but it is saying the answer is 6. Can some explain me? class Solution:   def maxSubarrayXOR (self, N, arr):       # code here        for i in range(N-1):           arr[i] = arr[i] ^ arr[i+1]       for j in range(len(arr)-1):           if arr[j] > arr[j+1]:               pass       return arr[j]       
0
sudarshans35 months ago
ss
-10
lapimpale7 months ago
class Solution{

    static int maxSubarrayXOR(int N, int arr[]){
        int ans = 1; // Initialize result 
       int curr_xor = 1; 
        // Pick starting points of subarrays 
        for (int i=0; i<N; i++) 
        {
                curr_xor = Math.max(arr[i],curr_xor^arr[i]); 
                ans = Math.max(ans, curr_xor); 
        } 
        return ans; 
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.