Skip to content

Instantly share code, notes, and snippets.

@arjitg
Created August 1, 2018 20:08
Show Gist options
  • Save arjitg/94498c6923e8e402c3db0db68cce3cfc to your computer and use it in GitHub Desktop.
Save arjitg/94498c6923e8e402c3db0db68cce3cfc to your computer and use it in GitHub Desktop.
InterviewBit Bit Manipulation Min XOR value
/*
Given an array of N integers, find the pair of integers in the array which have minimum XOR value. Report the minimum XOR value.
Examples :
Input
0 2 5 7
Output
2 (0 XOR 2)
Input
0 4 7 9
Output
3 (4 XOR 7)
Constraints:
2 <= N <= 100 000
0 <= A[i] <= 1 000 000 000
*/
int Solution::findMinXor(vector<int> &A) {
sort(A.begin(), A.end());
int ans = A[A.size()-1];
int x;
for(int i=0;i<A.size()-1;i++){
x = A[i]^A[i+1];
if(x<ans) ans=x;
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment