Problem Statement

Given an integer array A of N integers, find the pair of integers in the array which have minimum XOR value. Report the minimum XOR value.

Input Format:

`First and only argument of input contains an integer array A`

Output Format:

`return a single integer denoting minimum xor value`

Constraints:

2 <= N <= 100 000 0 <= A[i] <= 1 000 000 000

For Examples :

Example Input 1: A = [0, 2, 5, 7] Example Output 1: 2 Explanation: 0 xor 2 = 2 Example Input 2: A = [0, 4, 7, 9] Example Output 2: 3

Problem Link

Min XOR value - InterviewBit

Code

/*Sort the array.
Loop through comparing the adjacent elements as in sorted adjacent element, most significant bits will be similar and least significant bits will be 
different hence you will obtain a lower XOR value as compared to any other pair in sorted 
array*/

int Solution::findMinXor(vector<int> &A) {
    
    sort(A.begin(),A.end());
    int res=INT_MAX;
    for(int i=0;i<A.size()-1;i++)
    {
        res=min(res,A[i]^A[i+1]);
    }

    return res;
}