We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Python solution O(30*n+30*m) - time, O(30*n) - space for the trie of bits prefixes:
defmaxXor(arr,queries):trie=dict()cur=trie#build a trie of numbers' bits prefixesforainarr:forbitinmap(int,bin(a)[2:].zfill(30)):ifbitnotincur:cur[bit]={}cur=cur[bit]cur=trierez=[]#given a trie of bits prefixes and a query, do a greedy search on itforqinqueries:sub_rez=''forbitinmap(int,bin(q)[2:].zfill(30)):if1-bitincur:sub_rez+='1'cur=cur[1-bit]else:sub_rez+='0'cur=cur[bit]rez.append(int(sub_rez,2))cur=triereturnrez
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Xor
You are viewing a single comment's thread. Return to all comments →
Python solution
O(30*n+30*m)
- time,O(30*n)
- space for the trie of bits prefixes: