@@ -0,0 +1,2517 @@
+package problems;%0A%0A/**%0A * Given a list of numbers, a%5B0%5D, a%5B1%5D, ... , a%5BN-1%5D. Find the maximum result of%0A * a%5Bi%5D XOR a%5Bj%5D. All numbers in array are 32-bit unsigned integers. Example:%0A * 11100 11001 10100 10001 01010 01000 00100 00001 Solution: 10100 01010%0A * %0A * @author Yi%0A */%0A%0Apublic class LargestXorPair %7B%0A    // number of digits to consider%0A    // assuming the first digit is 0 (for positive)%0A    final int Digits = 6;%0A%0A    class Node %7B%0A        Node zero, one;%0A    %7D%0A%0A    final int maxXOR(final int%5B%5D A) %7B%0A        // Assume A contains all positive number (first digit 0)%0A        if (A.length %3C= 1)%0A            return 0;%0A%0A        Node root = new Node();%0A        for (final int k : A)%0A            insert(root, k);%0A%0A        int max = 0;%0A        for (int t : A) %7B%0A            int p = findPair(root, t);%0A            max = Math.max(max, t %5E p);%0A        %7D%0A        return max;%0A    %7D%0A%0A    final Node insert(final Node r, final int a) %7B%0A        Node n = r;%0A        for (int i = 1; i %3C Digits; i++) %7B%0A            if (digit1(a, i)) %7B%0A                if (n.one != null)%0A                    n = n.one;%0A                else %7B%0A                    Node newNode = new Node();%0A                    n.one = newNode;%0A                    n = newNode;%0A                %7D%0A            %7D else %7B%0A                if (n.zero != null)%0A                    n = n.zero;%0A                else %7B%0A                    Node newNode = new Node();%0A                    n.zero = newNode;%0A                    n = newNode;%0A                %7D%0A            %7D%0A        %7D%0A        return n;%0A    %7D%0A%0A    final boolean digit1(final int a, final int digit) %7B%0A        int musk = 1 %3C%3C (Digits - 1 - digit);%0A        return (a & musk) %3E 0;%0A    %7D%0A%0A    final int findPair(Node r, int a) %7B%0A        Node n = r;%0A%0A        int out = 0, base = 1;%0A        for (int i = 0; i %3C Digits - 2; i++)%0A            base *= 2;%0A%0A        for (int i = 1; i %3C Digits; i++) %7B%0A            if (n.zero == null %7C%7C (n.one != null && !digit1(a, i))) %7B%0A                n = n.one;%0A                out += base;%0A            %7D else%0A                n = n.zero;%0A%0A            base /= 2;%0A        %7D%0A        System.out.println(Integer.toBinaryString(a) + %22, %22 + Integer.toBinaryString(out));%0A        return out;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        LargestXorPair lx = new LargestXorPair();%0A%0A        final int%5B%5D A = %7B 0b11100, 0b11001, 0b10100, 0b10001, 0b01010, 0b01000, 0b00100, 0b00001 %7D;%0A%0A        final int max = lx.maxXOR(A);%0A        System.out.println(Integer.toBinaryString(max));%0A    %7D%0A%7D%0A

Add Largest XOR pair