max Xor of 2 number in array

pull/892/head
jayjariwala96 2022-10-02 19:38:14 +05:30
parent 62907b69ec
commit 0ba659b442
1 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,145 @@
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define for0(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define pb push_back
#define sz(x) sizeof(x)
#define vec(n, x) vector<int> 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<int> 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<<idx<<" ";
if (temp->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<int> nums(n);
Trie *t = new Trie();
for (auto &i : nums)
{
cin>>i;
t->insert(i);
}
int mx = 0;
// cout<<res;
for (auto i : nums)
{
int mask = INT_MAX;
int req = mask ^ i;
int res = t->query(req);
// cout<<res<<" ";
mx = max(mx, res ^ i);
}
cout << mx;
}