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.
classTrie{innerclassTrieNode{valchildren=Array<TrieNode?>(2){null}}valroot=TrieNode()funinsert(n:Int){varcurrentNode=rootfor(iin31downTo0){valx=((1shli)andn)shri// x is either 0 or 1if(currentNode.children[x]==null){currentNode.children[x]=TrieNode()}currentNode=currentNode.children[x]!!}}fungetMaximumXOR(q:Int):Int{varcurrentNode=rootvarans=0for(iin31downTo0){valv=1shlivalx=1-((vandq)shri)if(currentNode.children[x]!=null){ans=ans+vcurrentNode=currentNode.children[x]!!}elseif(currentNode.children[1-x]!=null){currentNode=currentNode.children[1-x]!!}}returnans}}// Complete the maxXor function below.funmaxXor(arr:Array<Int>,queries:Array<Int>):Array<Int>{valans=Array<Int>(queries.size){0}valtrie=Trie()for(vinarr){trie.insert(v)}for(iin0untilqueries.size){ans[i]=trie.getMaximumXOR(queries[i])}returnans}
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 →
Kotlin solution: