Maximum XOR subarray

Sairam Penjarla
1 min readMay 14, 2021

--

Given an array of integers of size N find subarray with maximum xor. A subbarray is a contiguous part of 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

from functools import reduce
def sub_lists_XOR(arr):
ans = -1
for i in range(len(arr) + 1):
for j in range(i):
ans = max(ans,reduce(lambda x, y: x ^ y, arr[j: i]))
return ans
l1 = [1, 2, 3,4]
print(sub_lists_XOR(l1))

#######--------or--------#######

def maxSubarrayXOR(arr):
N = len(arr)
ans = -1
for i in range(N):
xor = 0
for j in range(i, N):
xor = xor ^ arr[j]
ans = max(ans, xor)
return ans
l1 = [1, 2, 3,4]
print(maxSubarrayXOR(l1))

--

--

Sairam Penjarla

Looking for my next opportunity to make change in a BIG way