Problem solution · Java

Maximum Strong Pair XOR II

Maximum Strong Pair XOR II: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
72 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Maximum Strong Pair XOR II, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 72 lines of Java from the credited upstream file 2935.java.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Strong Pair XOR II · JavaJava
Use this to learn the idea, then write your own version.
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;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗