How do i approach this problem using trie?
Range XOR (Trie)
How do i approach this problem using trie?
How do i approach this problem using trie???
whenever you get query 1 create Trie insert array elements from l to r in it then find no which gives max xor with given no.
Hope it Helps!!
@Karanveer hey,
While solving this question using trie.
- At each node of the trie, we will store the ith bit of the number for a query of type 0.
- To solve the query of type 1 we will store the indexes of the number that pass through that node.
- When we go down the tree during a query and maximizing the xor, we go in a direction that contains at least one index in range L to R, otherwise, we will go in the other direction.
- To search if at least one index is within the stored indexes we will use binary search.
(Since the indices will always be positive, you can apply count sort on them. That will do your work in linear time.)
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.