From 0ba659b442ce966810989ff4ba3bbddc1ca8fdbe Mon Sep 17 00:00:00 2001 From: jayjariwala96 Date: Sun, 2 Oct 2022 19:38:14 +0530 Subject: [PATCH] max Xor of 2 number in array --- .../Trie/Max-Xor-of-Two-Number-Trie.cpp | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 algorithms/CPlusPlus/Trie/Max-Xor-of-Two-Number-Trie.cpp diff --git a/algorithms/CPlusPlus/Trie/Max-Xor-of-Two-Number-Trie.cpp b/algorithms/CPlusPlus/Trie/Max-Xor-of-Two-Number-Trie.cpp new file mode 100644 index 00000000..cb88b51b --- /dev/null +++ b/algorithms/CPlusPlus/Trie/Max-Xor-of-Two-Number-Trie.cpp @@ -0,0 +1,145 @@ +#include +using namespace std; +#define ll long long int +#define for0(i, n) for (int i = 0; i < n; i++) +#define vi vector +#define pb push_back +#define sz(x) sizeof(x) +#define vec(n, x) vector v(n, x) +const ll INF = 1e18; +const ll MOD = 1000000007; +#define hi \ + ll tests; \ + cin >> tests; \ + while (tests--) +#define all(x) x.begin(), x.end() +#define readv(vec) \ + for (auto &x : vec) \ + { \ + cin >> x; \ + } +#define PHI M_PI +#define graph \ + int V; \ + cin >> V; \ + vector adj[V]; \ + int e; \ + for (int i = 0; i < e; i++) \ + { \ + int u, v; \ + cin >> u >> v; \ + adj[u].push_back(v); \ + adj[v].push_back(u); \ + } +#define printv(vec) \ + for (auto x : vec) \ + { \ + cout << x << ' '; \ + } \ + cout << endl; +#define sortasc(vec) sort(all(vec)); +#define sortdes(vec) sort(vec.rbegin(), vec.rend()); +#define setpre(x) cout << fixed << setprecision(10) << x << endl +#define kick cout << 'C' << 'a' << 's' << 'e' << ' ' << '#' << k << ':' << ' ' +struct node +{ + node *left; + node *right; +}; +class Trie +{ +public: + node *root; + Trie() + { + root = new node(); + } + + void insert(int x) + { + node *temp = root; + int idx = 31; + while (idx >= 0) + { + int mask = (1 << idx); + if (x & mask) + { + // cout<right == NULL) + { + temp->right = new node(); + } + temp = temp->right; + } + else + { + if (temp->left == NULL) + { + temp->left = new node(); + } + temp = temp->left; + } + idx--; + } + } + int query(int x) + { + node *temp = root; + int idx = 31; + int sol = 0; + while (idx >= 0) + { + int mask = (1 << idx); + if ((mask & x)) + { + // cout<<"jo"; + if (temp->right == NULL) + { + temp = temp->left; + } + else + { + sol |= mask; + temp = temp->right; + } + } + else + { + if (temp->left == NULL) + { + sol |= mask; + temp = temp->right; + } + else + { + temp = temp->left; + } + } + idx--; + } + return sol; + } +}; +int main() +{ + int n; + cin >> n; + vector nums(n); + Trie *t = new Trie(); + for (auto &i : nums) + { + cin>>i; + t->insert(i); + } + int mx = 0; + // cout<query(req); + // cout<