Skip to content

2935. Maximum Strong Pair XOR II 👍

  • Time: $O(n\log_2(\max(\texttt{nums})))$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
struct TrieNode {
  vector<shared_ptr<TrieNode>> children;
  TrieNode() : children(2) {}
  int mn = INT_MAX;
  int mx = INT_MIN;
};

class BitTrie {
 public:
  BitTrie(int maxBit) : maxBit(maxBit) {}

  void insert(int num) {
    shared_ptr<TrieNode> node = root;
    for (int i = maxBit; i >= 0; --i) {
      const int bit = 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.
  int getMaxXor(int x) {
    int maxXor = 0;
    shared_ptr<TrieNode> node = root;
    for (int i = maxBit; i >= 0; --i) {
      const int bit = x >> i & 1;
      const int toggleBit = 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];
      } else if (node->children[bit] != nullptr) {
        node = node->children[bit];
      } else {  // There's nothing in the Bit Trie.
        return 0;
      }
    }
    return maxXor;
  }

 private:
  const int maxBit;
  shared_ptr<TrieNode> root = make_shared<TrieNode>();
};

class Solution {
 public:
  // Same as 2932. Maximum Strong Pair XOR I
  int maximumStrongPairXor(vector<int>& nums) {
    const int maxNum = ranges::max(nums);
    const int maxBit = static_cast<int>(log2(maxNum));
    int ans = 0;
    BitTrie bitTrie(maxBit);

    for (const int num : nums)
      bitTrie.insert(num);

    for (const int num : nums)
      ans = max(ans, bitTrie.getMaxXor(num));

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class TrieNode {
  public TrieNode[] children = new TrieNode[2];
  public int mn = Integer.MAX_VALUE;
  public int mx = Integer.MIN_VALUE;
}

class BitTrie {
  public BitTrie(int maxBit) {
    this.maxBit = maxBit;
  }

  public void insert(int num) {
    TrieNode node = root;
    for (int i = maxBit; i >= 0; --i) {
      final int bit = (int) (num >> i & 1);
      if (node.children[bit] == null)
        node.children[bit] = new TrieNode();
      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.
  public int getMaxXor(int x) {
    int maxXor = 0;
    TrieNode node = root;
    for (int i = maxBit; i >= 0; --i) {
      final int bit = (int) (x >> i & 1);
      final int toggleBit = 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];
      } else if (node.children[bit] != null) {
        node = node.children[bit];
      } else { // There's nothing in the Bit Trie.
        return 0;
      }
    }
    return maxXor;
  }

  private int maxBit;
  private TrieNode root = new TrieNode();
}

class Solution {
  // Same as 2932. Maximum Strong Pair XOR I
  public int maximumStrongPairXor(int[] nums) {
    final int maxNum = Arrays.stream(nums).max().getAsInt();
    final int maxBit = (int) (Math.log(maxNum) / Math.log(2));
    int ans = 0;
    BitTrie bitTrie = new BitTrie(maxBit);

    for (final int num : nums)
      bitTrie.insert(num);

    for (final int num : nums)
      ans = Math.max(ans, bitTrie.getMaxXor(num));

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class TrieNode:
  def __init__(self):
    self.children: list[TrieNode | None] = [None] * 2
    self.mn = math.inf
    self.mx = -math.inf


class BitTrie:
  def __init__(self, maxBit: int):
    self.maxBit = maxBit
    self.root = TrieNode()

  def insert(self, num: int) -> None:
    node = self.root
    for i in range(self.maxBit, -1, -1):
      bit = num >> i & 1
      if not node.children[bit]:
        node.children[bit] = TrieNode()
      node = node.children[bit]
      node.mn = min(node.mn, num)
      node.mx = max(node.mx, num)

  def getMaxXor(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 = 0
    node = self.root
    for i in range(self.maxBit, -1, -1):
      bit = x >> i & 1
      toggleBit = 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] and
          node.children[toggleBit].mx > x and
              node.children[toggleBit].mn <= 2 * x):
        maxXor = maxXor | 1 << i
        node = node.children[toggleBit]
      elif node.children[bit]:
        node = node.children[bit]
      else:  # There's nothing in the Bit Trie.
        return 0
    return maxXor


class Solution:
  # Same as 2932. Maximum Strong Pair XOR I
  def maximumStrongPairXor(self, nums: list[int]) -> int:
    maxNum = max(nums)
    maxBit = int(math.log2(maxNum))
    bitTrie = BitTrie(maxBit)

    for num in nums:
      bitTrie.insert(num)

    return max(bitTrie.getMaxXor(num) for num in nums)