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.
Most useful comment without giving an exact solution, thanks @dcgibbons !
Based on this here is my (hopefully clear) javascript solution (passing all tests):
constnodeOf=bit=>({bit,'0':null,'1':null});constopposite=bit=>bit==='1'?'0':'1';consttoBinary=number=>number.toString(2).padStart(32,0);consttoDecimal=binary=>parseInt(binary,2);functioninsert(node,binary){consthead=binary[0];consttail=binary.substring(1);if(!node[head]){node[head]=nodeOf(head);}if(tail){insert(node[head],tail);}}functionbuildTrie(numbers){constroot=nodeOf(null);numbers.forEach(value=>insert(root,toBinary(value)));returnroot;}functionfindBest(node,binary){consthead=binary[0];consttail=binary.substring(1);// the best is the opposite (xor -> 1) at each stage but we move forward until we can anywayconstnext=node[opposite(head)]||node[head];if(next){returnnext.bit+findBest(next,tail);}return'';}functionmaxXor(arr,queries){constroot=buildTrie(arr);returnqueries.map(query=>query^toDecimal(findBest(root,toBinary(query))));}
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 →
Most useful comment without giving an exact solution, thanks @dcgibbons !
Based on this here is my (hopefully clear) javascript solution (passing all tests):