structTrieNode{vector<shared_ptr<TrieNode>>children;TrieNode():children(2){}intmn=INT_MAX;intmx=INT_MIN;};classBitTrie{public:BitTrie(intmaxBit):maxBit(maxBit){}voidinsert(intnum){shared_ptr<TrieNode>node=root;for(inti=maxBit;i>=0;--i){constintbit=num>>i&1;if(node->children[bit]==nullptr)node->children[bit]=make_shared<TrieNode>();node=node->children[bit];node->mn=min(node->mn,num);node->mx=max(node->mx,num);}}// Returns max(x ^ y), where |x - y| <= min(x, y).//// If x <= y, |x - y| <= min(x, y) can be written as y - x <= x.// So, y <= 2 * x.intgetMaxXor(intx){intmaxXor=0;shared_ptr<TrieNode>node=root;for(inti=maxBit;i>=0;--i){constintbit=x>>i&1;constinttoggleBit=bit^1;// If `node.children[toggleBit].max > x`, it means there's a number in the// node that satisfies the condition to ensure that x <= y among x and y.// If `node.children[toggleBit].min <= 2 * x`, it means there's a number// in the node that satisfies the condition for a valid y.if(node->children[toggleBit]!=nullptr&&node->children[toggleBit]->mx>x&&node->children[toggleBit]->mn<=2*x){maxXor=maxXor|1<<i;node=node->children[toggleBit];}elseif(node->children[bit]!=nullptr){node=node->children[bit];}else{// There's nothing in the Bit Trie.return0;}}returnmaxXor;}private:constintmaxBit;shared_ptr<TrieNode>root=make_shared<TrieNode>();};classSolution{public:// Same as 2932. Maximum Strong Pair XOR IintmaximumStrongPairXor(vector<int>&nums){constintmaxNum=ranges::max(nums);constintmaxBit=static_cast<int>(log2(maxNum));intans=0;BitTriebitTrie(maxBit);for(constintnum:nums)bitTrie.insert(num);for(constintnum:nums)ans=max(ans,bitTrie.getMaxXor(num));returnans;}};
classTrieNode{publicTrieNode[]children=newTrieNode[2];publicintmn=Integer.MAX_VALUE;publicintmx=Integer.MIN_VALUE;}classBitTrie{publicBitTrie(intmaxBit){this.maxBit=maxBit;}publicvoidinsert(intnum){TrieNodenode=root;for(inti=maxBit;i>=0;--i){finalintbit=(int)(num>>i&1);if(node.children[bit]==null)node.children[bit]=newTrieNode();node=node.children[bit];node.mn=Math.min(node.mn,num);node.mx=Math.max(node.mx,num);}}// Returns max(x ^ y), where |x - y| <= min(x, y).//// If x <= y, |x - y| <= min(x, y) can be written as y - x <= x.// So, y <= 2 * x.publicintgetMaxXor(intx){intmaxXor=0;TrieNodenode=root;for(inti=maxBit;i>=0;--i){finalintbit=(int)(x>>i&1);finalinttoggleBit=bit^1;// If `node.children[toggleBit].mx > x`, it means there's a number in the// node that satisfies the condition to ensure that x <= y among x and y.// If `node.children[toggleBit].mn <= 2 * x`, it means there's a number// in the node that satisfies the condition for a valid y.if(node.children[toggleBit]!=null&&node.children[toggleBit].mx>x&&node.children[toggleBit].mn<=2*x){maxXor=maxXor|1<<i;node=node.children[toggleBit];}elseif(node.children[bit]!=null){node=node.children[bit];}else{// There's nothing in the Bit Trie.return0;}}returnmaxXor;}privateintmaxBit;privateTrieNoderoot=newTrieNode();}classSolution{// Same as 2932. Maximum Strong Pair XOR IpublicintmaximumStrongPairXor(int[]nums){finalintmaxNum=Arrays.stream(nums).max().getAsInt();finalintmaxBit=(int)(Math.log(maxNum)/Math.log(2));intans=0;BitTriebitTrie=newBitTrie(maxBit);for(finalintnum:nums)bitTrie.insert(num);for(finalintnum:nums)ans=Math.max(ans,bitTrie.getMaxXor(num));returnans;}}
classTrieNode:def__init__(self):self.children:list[TrieNode|None]=[None]*2self.mn=math.infself.mx=-math.infclassBitTrie:def__init__(self,maxBit:int):self.maxBit=maxBitself.root=TrieNode()definsert(self,num:int)->None:node=self.rootforiinrange(self.maxBit,-1,-1):bit=num>>i&1ifnotnode.children[bit]:node.children[bit]=TrieNode()node=node.children[bit]node.mn=min(node.mn,num)node.mx=max(node.mx,num)defgetMaxXor(self,x:int)->int:"""Returns max(x ^ y) where |x - y| <= min(x, y). If x <= y, |x - y| <= min(x, y) can be written as y - x <= x. So, y <= 2 * x. """maxXor=0node=self.rootforiinrange(self.maxBit,-1,-1):bit=x>>i&1toggleBit=bit^1# If `node.children[toggleBit].mx > x`, it means there's a number in the# node that satisfies the condition to ensure that x <= y among x and y.# If `node.children[toggleBit].mn <= 2 * x`, it means there's a number in# the node that satisfies the condition for a valid y.if(node.children[toggleBit]andnode.children[toggleBit].mx>xandnode.children[toggleBit].mn<=2*x):maxXor=maxXor|1<<inode=node.children[toggleBit]elifnode.children[bit]:node=node.children[bit]else:# There's nothing in the Bit Trie.return0returnmaxXorclassSolution:# Same as 2932. Maximum Strong Pair XOR IdefmaximumStrongPairXor(self,nums:list[int])->int:maxNum=max(nums)maxBit=int(math.log2(maxNum))bitTrie=BitTrie(maxBit)fornuminnums:bitTrie.insert(num)returnmax(bitTrie.getMaxXor(num)fornuminnums)
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! π