The code below is a fixed version of my initial post thanks to @SergeyTachenov 's thorough analysis of the problem. In particular he indicated that the previous version fails for the following test cases:

`[4,6,7]`

, `[8,10,2]`

, `[14, 15, 9, 3, 2]`

and `[15, 15, 9, 3, 2]`

which are currently not present in OJ and should be included.

The current version, below works for all of these cases. Check out also @SergeyTachenov 's version here.

```
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
if (nums.size() < 2) {
return 0;
} else if (nums.size() == 2) {
return nums[0] ^ nums[1];
}
auto max = *max_element(nums.begin(), nums.end());
auto msb = static_cast<int>(log2(max));
auto msbSplit
= partition(nums.begin(), nums.end(), [&](const int& n) { return (n & (1 << msb)) != 0; });
while (msbSplit == nums.begin() || msbSplit == nums.end()) {
--msb;
if (msb == 0) {
return 0;
}
msbSplit
= partition(nums.begin(), nums.end(), [&](const int& n) { return (n & (1 << msb)) != 0; });
}
return findMaximumXor(nums.begin(), msbSplit, msbSplit, nums.end(), msb);
}
int findMaximumXor(const vector<int>::iterator& beginLeft,
const vector<int>::iterator& endLeft,
const vector<int>::iterator& beginRight,
const vector<int>::iterator& endRight,
int msb) {
if (beginLeft == endLeft|| beginRight == endRight) {
return 0;
}
if (msb == 0 || (distance(beginLeft, endLeft) == 1 && distance(beginRight, endRight) == 1)) {
return *beginLeft ^ *beginRight;
}
auto mask = 1 << (msb - 1);
auto splitLeft
= partition(beginLeft, endLeft, [&](const int& n) { return (n & mask) != 0; });
auto splitRight
= partition(beginRight, endRight, [&](const int& n) { return (n & mask) != 0; });
auto result1 = findMaximumXor(beginLeft, splitLeft, splitRight, endRight, msb - 1);
auto result2 = findMaximumXor(splitLeft, endLeft, beginRight, splitRight, msb - 1);
auto result = max(result1, result2);
if (result == 0) {
result = findMaximumXor(beginLeft, endLeft, beginRight, endRight, msb - 1);
}
return result;
}
};
```