C++, 22 ms, beats 99.5%, array partitioning similar to quick sort
I have put detailed comments in the code. Here I just highlight the logic of the method.\n\n1) working from most significant bit on the left towards right. Obvi
I have put detailed comments in the code. Here I just highlight the logic of the method.\n\n1) working from most significant bit on the left towards right. Obviously, if the more significant bit is 1, the xor value is greater than those with this bit = 0.\n2) Similar to quick sort, we partition a certain range of the array nums in place. The left subrange has current bit = 1, and the right subrange has current bit = 0. Let's name them as A and B.\n3) In order to find the greatest XOR value, we have to take 1 number from A, and 1 number from B. And to set next bit to 1, there must be a subrange in A and a subrange in B having opposite bit; otherwise, next bit will be 0. So we partition range A and B recursively.\n4) When partitioning range A and B, there are 3 cases. (1) A has both bit 1 and 0 ranges. We check whether B has bit 0 or bit 1 range or both. (2) A has only bit 1. We check whether B has bit 0. (3) A has only bit 0. We check whether B has bit 1.\n\nFor every recursive call, the runtime is O(n). There are totally 31 levels of calls, so the runtime is O(n). And extra space is O(1) because the code works in place. The code with comments is as below.\n```\nclass Solution {\npublic:\n    int findMaximumXOR(vector<int>& nums) {\n        int n = nums.size();\n        return helper(nums, 0, n-1, 0, n-1, 0, 30);\n    }\nprivate:\n    // (ls, le) and (rs, re) are two ranges of nums, which gives max xor value to current bit;\n    // bit decreases from 30 to 0, i.e., working from most significant bit on the left towards right;\n    // Similar to quicksort, partition (ls, le) to two ranges (ls, j-1) and (j, le) by swapping elements\n    // the range on the left with current bit = 1, and the range on right is 0; We do the same to (rs, re)\n    // In order to set the current bit in the answer, i.e. val, to be 1, the left (ls, le) and right (rs,re) ranges must have subranges with opposite bit. If so, val = (val << 1) + 1; otherwise, val = val << 1.\n    int helper(vector<int>& nums, int ls, int le, int rs, int re, int val, int bit) {\n        if (bit == -1) return val;\n        int mask = 1<<bit, j = ls, k = rs;\n        for (int i = ls; i <= le; i++) \n            if (nums[i]&mask) swap(nums[i], nums[j++]);\n        for (int i = rs; i <= re; i++) \n            if (nums[i]&mask) swap(nums[i], nums[k++]);\n        // the left range has two subranges, the answer is max of (bit 1 subrange on the left and bit 0 subrange on the right) or (bit 0 subrange on the left and bit 1 subrange on the right)\n        if (j > ls && j <= le) {\n            int ans = 0;\n            if (k > rs) \n                ans = helper(nums, j, le, rs, k-1, val*2+1, bit-1);\n            if (k <= re) \n                ans = max(ans, helper(nums, ls, j-1, k, re, val*2+1, bit-1));\n            return ans;\n        }\n        // the left range has only bit 0 subrange\n        else if (j <= ls) {\n            // check whether the right range has bit 1 subrange\n            if (k > rs) \n                return helper(nums, ls, le, rs, k-1, val*2+1, bit-1);\n            else \n                return helper(nums, ls, le, rs, re, val*2, bit-1);\n        }\n        // the left range has only bit 1 subrange\n        else {\n            // check whether the right range has bit 0 subrange\n            if (k <= re) \n                return helper(nums, ls, le, k, re, val*2+1, bit-1);\n            else \n                return helper(nums, ls, le, rs, re, val*2, bit-1);\n        }\n    }\n};\n```
[]