Maximum XOR of Two Numbers in an Array

Given an array Arr of non-negative integers of size N. The task is to find the maximum possible xor between two numbers present in the array.

Example

Input: Arr = {25, 10, 2, 8, 5, 3} 
Output: 28 
The maximum result is 5 ^ 25 = 28 

Input: Arr = {1, 2, 3, 4, 5, 6, 7} 
Output:
The maximum result is 1 ^ 6 = 7 

Naive Approach: A Simple Solution is to generate all pairs of the given array and compute XOR of the pairs. Finally, return maximum XOR value. This solution takes 



O(N^{2}) 

time.

Below is the implementation of the above approach: 

C++

// C++ implementation
#include <bits/stdc++.h>
using namespace std;

// Function to return the
// maximum xor
int max_xor(int arr[], int n)
{

    int maxXor = 0;
  
    // Calculating xor of each pair
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++) 
        {
            maxXor = max(maxXor, arr[i] ^ arr[j]);
        }
    }
    return maxXor;
}

// Driver Code
int main()
{
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    // Function call
    cout << max_xor(arr, n) << endl;
    return 0;
}

Java

// Java implementation of the approach
class GFG 
{

    // Function to return the
    // maximum xor
    static int max_xor(int arr[], int n)
    {
        int maxXor = 0;

        // Calculating xor of each pair
        for (int i = 0; i < n; i++) 
        {
            for (int j = i + 1; j < n; j++) 
            {
                maxXor = Math.max(maxXor, arr[i] ^ arr[j]);
            }
        }
        return maxXor;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 25, 10, 2, 8, 5, 3 };
        int n = arr.length;

        // Function call
        System.out.println(max_xor(arr, n));
    }
}

// This code is contributed by Rajput-Ji

Python3

# Python3 implementation

# Function to return the
# maximum xor

def max_xor(arr, n):

    maxXor = 0

    # Calculating xor of each pair
    for i in range(n):
        for j in range(i + 1, n):
            maxXor = max(maxXor,
                         arr[i] ^ arr[j])

    return maxXor


# Driver Code
if __name__ == '__main__':

    arr = [25, 10, 2, 8, 5, 3]
    n = len(arr)

    # Function call
    print(max_xor(arr, n))

# This code is contributed by 29AjayKumar

C#

// C# implementation of the approach
using System;

class GFG {

    // Function to return the
    // maximum xor
    static int max_xor(int[] arr, int n)
    {
        int maxXor = 0;

        // Calculating xor of each pair
        for (int i = 0; i < n; i++) 
        {
            for (int j = i + 1; j < n; j++) 
            {
                maxXor = Math.Max(maxXor, arr[i] ^ arr[j]);
            }
        }
        return maxXor;
    }

    // Driver Code
    public static void Main()
    {
        int[] arr = { 25, 10, 2, 8, 5, 3 };
        int n = arr.Length;

        // Function call
        Console.WriteLine(max_xor(arr, n));
    }
}

// This code is contributed by AnkitRai01