Maximum Xor

Sort by

recency

|

61 Discussions

|

  • + 0 comments

    I thought this website was about real world problems, this is not what most people are doing in their

  • + 0 comments

    In swift. At first I add submit code and I failed in only test case 5 then I do nothing any change and jst again click submit code I passed in all test cases.

    class TrieNode {
    var children = Array<TrieNode?>(repeating: nil, count: 2)
    
    func add(value: Int, index: Int) {
            let bitPos = 31 - index
      if bitPos < 0 { return }
    
      let power = 1 << bitPos
      let bit = (value & power) >> bitPos // Extract the bit at the current position
      let remainder = value & (power - 1) // Calculate the remainder
    
      if let child = children[bit] {
                child.add(value: remainder, index: index + 1)
      } else {
                children[bit] = TrieNode()
        children[bit]!.add(value: remainder, index: index + 1)
            }
    }
    }
    
    struct BitTrie {
        let root = TrieNode()
    
    func add(_ value: Int) {
            root.add(value: value, index: 0)
    }
    
    func getMaxXor(_ value: Int) -> Int {
            var current = root
      var xorValue = 0
      var remainder = value
    
      for i in 0..<32 {
                let bitPos = 31 - i
        let power = 1 << bitPos
        let bit = (remainder & power) >> bitPos // Extract the bit at the current position
        remainder = remainder & (power - 1) // Calculate the remainder
    
        if let child = current.children[bit ^ 1] {
                    xorValue += power
          current = child
                 } else {
                     current = current.children[bit]!
         }
       }
    
       return xorValue
            }
        }
    
    // Complete the maxXor function below.
    func maxXor(arr: [Int], queries: [Int]) -> [Int] {
        var maxVals = [Int]()
    let trie = BitTrie()
    
    for value in arr {
            trie.add(value)
    }
    
    for query in queries {
            maxVals.append(trie.getMaxXor(query))
    }
    
    return maxVals
    }
    
  • + 0 comments

    Python solution O(30*n+30*m) - time, O(30*n) - space for the trie of bits prefixes:

    def maxXor(arr, queries):
        trie = dict()
        cur = trie
        #build a trie of numbers' bits prefixes
        for a in arr:
            for bit in map(int, bin(a)[2:].zfill(30)):
                if bit not in cur:
                    cur[bit] = {}
                cur = cur[bit]
            cur = trie
        
        rez = []
        #given a trie of bits prefixes and a query, do a greedy search on it
        for q in queries:
            sub_rez = ''
            for bit in map(int, bin(q)[2:].zfill(30)):
                if 1 - bit in cur:
                    sub_rez += '1'
                    cur = cur[1 - bit]
                else:
                    sub_rez += '0'
                    cur = cur[bit]
            rez.append(int(sub_rez, 2))
            cur = trie
        return rez
    
  • + 0 comments

    Java, O(n+m) - time, O(n) - space.

    static int[] maxXor(int[] arr, int[] queries) {
        int[] answer = new int[queries.length];
        Node root = new Node();
        for (int i : arr) root.add(i, 30);
        for (int i = 0; i < queries.length; i++)
            answer[i] = queries[i] ^ root.get(queries[i], 30);
        return answer;
    }
    
    static class Node {
        Node zero;
        Node one;
        
        void add(int num, int bit) {
            if (bit == -1) return;
            if (((num >> bit) & 1) == 0) {
                if (zero == null) zero = new Node();
                zero.add(num, bit - 1);
            } else {
                if (one == null) one = new Node();
                one.add(num, bit - 1);
            }
        }
        
        int get(int num, int bit) {
            if (bit == -1) return 0;
            if (((num >> bit) & 1) == 0 && one != null || zero == null)
                return (1 << bit) ^ one.get(num, bit - 1);
            else
                return (0 << bit) ^ zero.get(num, bit - 1);
        }
    }
    
  • + 0 comments

    hint: most sig bit gives larger number