Range xor (2 testcases are not passing )


I think my logic is not correct so please do describe the logic or any error in my code

@Saksham12 You can have a look at this approach. It might help you.
typedef struct data
{
data* bit[2];
int cnt = 0;
}trie;

trie* head;

void insert(int x)
{
trie* cur = head;
for(int i=30;i>=0;i–)
{
int b = (x>>i) & 1;
if(!cur->bit[b])
cur->bit[b] = new trie();
cur = cur->bit[b];
cur->cnt++;
}
}

void remove(int x)
{
trie* cur = head;
for(int i=30;i>=0;i–)
{
int b = (x>>i) & 1;
cur = cur->bit[b];
cur->cnt–;
}
}

int maxxor(int x)
{
trie* cur = head;
int ans = 0;
for(int i=30;i>=0;i–)
{
int b = (x>>i)&1;
if(cur->bit[!b] && cur->bit[!b]->cnt>0)
{
ans += (1LL<<i);
cur = cur->bit[!b];
}
else
cur = cur->bit[b];
}
return ans;
}

Thanks for the code but can you explain the approach through words because then it will be easy to understand

@Saksham12 there’s no much difference. In words you have 26 letters and in bits we have 2 states 0&1. So we have created an array of pointers of size 2 instead of 26.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.