Java simple greedy + bit manipulation solution, O(2^n) time , with explanation (without HashMap)
Hi there! I am posting my recursive solution based on bit manipulation. The idea is strightforward. Let us think about the resultant number. Well, it is clear t
Hi there! I am posting my recursive solution based on bit manipulation. The idea is strightforward. Let us think about the resultant number. Well, it is clear that, in order to, maximize the resultant number, we have to maximize the number of set bits with higher order. In other words, starting from the left most possible bit position we check if there is a pair of numbers, such that their bits in the current position will   XOR\'ed up to 1. Obviously there are only two options to satisfy that condition 1,0 or 0,1 for each bit position. Thus, we always divide the numbers into two parts, those that has set bits in current position and those that has zero bits in current position. Consequently if we can divide the numbers in such a way, it means we can obtain set bit by some combination in the current position. Otherwise, we decrease factor of two ( bit position) while we can\'t divide the numbers and we didn\'t reach the last bit position. For example \n```\n                       {1,2,3} <- bit position 1\n                        /    \\\n                    {1}      {2, 3} <- bit position 0\n                    / \\        /  \\\n                  X   {1}   {2}   {3}    \n```\nIn the example above, given array {1,2,3} we can start from bit position 1, because it is the highest bit order in current array. Then we divide it according to the values of current bit. Correspondingly {1} has 0 bit in position 1, whereas 2 and 3 have set bits. It means our resultant number has set bit in position 1. Next we divide the halves into halves similarly for bit position 0. Here you can see that array {1} does not have numbers which bits in position 0 are not set, but has a number which has set bit in that position({1}). In turn the right part is divided to {2} and {3} correspondingly, because 2 has 0 bit in 0th position and 3 has set bit in 0th position. That fact means that, we can also XOR 1 with 2 and get set bit in position 1, therefore the answer is 3 = 2^1+2^0;\n Actually the running time of the algorithm isO(n^2) and it is slow. Even very naive solution is faster than that.\n```\npublic class Solution {\n    public int findMaximumXOR(int[] nums) {\n        int max = 0;\n        if(nums == null || nums.length == 0) return 0;\n        List<Integer> left = new ArrayList<>(), right = new ArrayList<>();\n        \n        for(int i = 0;i<nums.length;i++){\n            max= Math.max(max, nums[i]);\n        }\n        int p = 31;\n        while(p>0 && (max & (1<<p)) == 0) p--;\n        int fact = 0;\n        while(p>=0 && (left.isEmpty() || right.isEmpty())){\n            fact = 1<<p;\n            for(int i = 0;i<nums.length;i++){\n                if((nums[i] & fact) == 0){\n                    left.add(nums[i]);\n                } else {\n                    right.add(nums[i]);\n                }\n            }\n            p--;\n        }\n        if(left.isEmpty() || right.isEmpty()) fact = 0;\n        // System.out.println(fact);\n        return fact+maxXOR(p, left, right);\n    }\n    \n    public int maxXOR(int p, List<Integer> left, List<Integer> right){\n        if(p<0) return 0;\n        // int fact;\n        List<Integer> leftZero  = new ArrayList<>(), rightZero  = new ArrayList<>();\n        List<Integer> leftOne = new ArrayList<>(), rightOne = new ArrayList<>();\n        int fact = 1<<p;\n        \n        while(p>=0 && ((leftZero.isEmpty() || rightOne.isEmpty()) && (leftOne.isEmpty() || rightZero.isEmpty()))){\n            fact = 1<<p;\n            leftZero.clear();\n            leftOne.clear();\n            rightZero.clear();\n            rightOne.clear();\n            fill(fact, left, leftZero, leftOne);\n            fill(fact, right, rightZero, rightOne);\n            p--;\n        }\n        if(((leftZero.isEmpty() || rightOne.isEmpty()) && (leftOne.isEmpty() || rightZero.isEmpty()))) {\n            return 0;\n        }\n        // System.out.println("+"+fact);\n        return fact+Math.max(maxXOR(p, leftZero, rightOne), maxXOR(p, rightZero, leftOne)); \n    }\n    \n    public void fill(int fact, List<Integer> list, List<Integer> zero, List<Integer> one){\n        for(Integer num:list){\n            if((fact & num) == 0) {\n                zero.add(num);\n            } else {\n                one.add(num);\n            }\n        }\n    }\n}
[]