Skip to main content

Maximum XOR of Two Numbers in an Array

Question

Given an array of integers, find the maximum possible value of the XOR of any two of its elements.

Example 1
Input: [3, 10, 5, 25, 2, 8]

Output: 28 (The maximum XOR of two numbers in the array is 25 XOR 3 = 28)

Solution

all//Maximum XOR of Two Numbers in an Array.py


def maxXor(arr):
maxXorValue = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)):
maxXorValue = max(maxXorValue, arr[i] ^ arr[j])
return maxXorValue

arr = [9, 5, 10, 7]
print(maxXor(arr)) # Outputs 15