Maximum value of 2 numbers from array xored by X

Given an array a[] of size N and an integer X. We want to return the maximum value of a[i] xor a[j] xor X.

You are given an array of integers of size 1 <= N <= 10^5 and an integer 1 <= X <= 10^5.

With maximum O(N log N) complexity we want to find greatest value of a[i] xor a[j] xor X.

Ex. In: a[] = {0,1,2} X=2 Binary representation of these numbers is 2 = 10, 1 = 01, 0 = 00 Maximum value we can get is 1 xor 0 xor X which is equal to 3

Read more here:

Content Attribution

This content was originally published by drandyl at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: