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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Maximum Xor
  2. Discussions

Maximum Xor

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • barretm99chaos
    3 years ago+ 3 comments

    my concise solution wich runs in O(n):

    def maxXor(arr, queries):
        ans = []
        trie = {}
        k = len(bin(max(arr+queries))) - 2 
        for number in ['{:b}'.format(x).zfill(k) for x in arr]:
            node = trie
            for char in number:
                node = node.setdefault(char, {})
        for n in queries:
            node = trie
            s = ''
            for char in'{:b}'.format(n).zfill(k) :
                tmp = str(int(char) ^ 1) 
                tmp = tmp if tmp in node else char 
                s += tmp 
                node = node[tmp]
            ans.append(int(s, 2) ^ n) 
        return ans
    
    19|
    Permalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature