A deatiled EXPLANATION that ever exists! With notes you can Download
\nGuy\'s I have one question for you in comment section, read the Article first. If you got time then, please reply back to my question\n\nNotes shown below, pd
```\nGuy\'s I have one question for you in comment section, read the Article first. If you got time then, please reply back to my question\n```\nNotes shown below, [**pdf link**](https://bit.ly/3H0y7Bf) download it!\n![image](https://assets.leetcode.com/users/images/819efa6f-744c-4dea-a425-f76a0d5adaf9_1643269548.7523139.jpeg)\n\n![image](https://assets.leetcode.com/users/images/31ab073b-f902-4526-89cf-525d2b58c2fe_1643269590.0578642.jpeg)\n\n![image](https://assets.leetcode.com/users/images/69fed067-fba6-4b5b-a6d4-62e21c87e6a8_1643269594.6708877.jpeg)\n\n![image](https://assets.leetcode.com/users/images/ead41dec-cea1-4fd0-8803-b6aeda81d6d9_1643269599.6864133.jpeg)\n\n\n```\nConclustion to this is, we want a data structure like, who help\'s in prefix search & that guy is "TRIE"\n```\n\nLet\'s Understand Trie. With taking an exaple:\n```\n00010\n01001\n10001\n10010\n10100\n10000\n00101\n```\n***Example, how\'s trie tree looks like:***\n\n![image](https://assets.leetcode.com/users/images/32b81a3f-9ed6-44ac-8d67-1e37110e4ac5_1643265330.8398294.png)\n\n`So, let\'s see how\'s our tree will be build, with given values`\n\n![image](https://assets.leetcode.com/users/images/0b42b9aa-8807-4848-8d5d-03fa30c1ff5c_1643266561.1381478.gif)\n\nTill Now, we have put all the no\'s on trie.\nNow we will pick every element and calcuate how much XOR could be possible.\n\nLet\'s calculate for **00010**\n\n![image](https://assets.leetcode.com/users/images/8575bb6a-40de-42c8-b77f-1ab896b888e7_1643271168.8860803.gif)\n\n\n```\na ->    00010\nfind -> 11101\nb ->    10100       [that we had found]\n----------------\nans ->  10110       [answer we got after performing XOR]\n----------------\n```\nNow, we will store this **answer** as our **max possible** till now i.e. **10110**.\n\nAnd similar for calculation of **01001**\n```\na ->    01001\nfind -> 10110\nb ->    10100       [that we had found]\n----------------\nans ->  11101       [answer we got after performing XOR]\n----------------\n```\nNow, we will store this **answer** as our **max possible** till now i.e. **11101** which is more then previous one i.e. **10110**\n\nI hope you got the idea, how we are doing is using our **trie** we are **traversing**. The no. we are **finding** `if exists we go there, if not exist whatever is present we use that `\n\n**Conclusion:**\n* Put every element on our trie\n* Now on which no. you are get it\'s complement part\n*  * Eg :- **a -> 10001**\n*  * Complement part to make **find -> 01110**\n*  On trie we have one functionality of insert & other of query(find) which jobs is to find that no. \n*  It will go anf find what we want and what is available over there.\n\nI hope now, approach is absolute clear.\n\n```\nGuy\'s code will be available soon.\nMy health is not A-OK. I have fever and my body temp. is 103 F (39.4 C)\nThat\'s why it took a lot time to provide you this explanation more than 5+ hours\n```\nOnce code is available i will update you! Thanks, for understanding my situation!\n\n**Update :**\n\n**Brute-Force Approach :** `[Time-Limit Exceed]`\n```\nclass Solution {\n    public int findMaximumXOR(int[] nums) {\n        int max = 0;\n        for(int i = 0; i < nums.length - 1; i++){\n            for(int j = i + 1; j < nums.length; j++){\n                max = Math.max(max, nums[i] ^ nums[j]);\n            }\n        }\n        return max;\n    }\n}\n```\n**Optimise-Approach :**\n```\nclass Solution {\n    public static class Trie{\n        /* we kind of use binary tree concept of left & right\n        X\n       / \\\n    L 0   1 R\n      */\n        public class Node{\n            Node left, right;\n        }\n        Node root;\n        Trie(){\n            root = new Node();\n        }\n        public void insert(int val){\n            int bitIndex = 30; // 0 ---- 30 [31] bit\'s\n            Node curr = root;\n            \n            while(bitIndex >= 0){\n                int mask = 1<<bitIndex;\n                int bit = (mask&val) > 0 ? 1 : 0;\n                // Till now our root is empty,\n                if(bit == 0){ // but if we get 0 we will add it to it\'s left\n                    if(curr.left == null){\n                        curr.left = new Node();\n                    }\n                    curr = curr.left;\n                }\n                else{ // if we get 1 we will add it to it\'s right \n                    if(curr.right == null){\n                        curr.right = new Node();\n                    }\n                    curr = curr.right;\n                }\n                bitIndex--;\n            }\n        }\n        public int query(int find){\n            int bitIndex = 30; // 0 ---- 30 [31] bit\'s\n            Node curr = root;\n            int ans = 0;\n            while(bitIndex >= 0){\n                int mask = 1<<bitIndex;\n                int bit = (find&mask) > 0 ? 1 : 0;\n                \n                if(bit == 0){\n                    if(curr.left != null){\n                        curr = curr.left;\n                    }\n                    else{\n                        curr = curr.right;\n                        ans |= mask;\n                    }\n                }\n                else{\n                    if(curr.right != null){\n                        curr = curr.right;\n                        ans |= mask;\n                    }\n                    else{\n                        curr = curr.left;\n                    }\n                }\n                \n                bitIndex--;\n            }\n            return ans;\n        } \n    }\n    public int findMaximumXOR(int[] nums) {\n        // Actuall logic\n        Trie trie = new Trie();\n        \n        for(int val : nums){\n            trie.insert(val);\n        }\n        \n        int max = 0;\n        for(int a : nums){\n            int find = Integer.MAX_VALUE ^ a;\n            int b = trie.query(find);\n            max = Math.max(max, a ^ b);\n        }\n        return max;\n    }\n}\n```
[]