Prefix tree of algorithm — Maximum XOR sum

Time:2020-11-21

The maximum XOR value of two numbers in an array

difficultysecondaryPrefix tree of algorithm -- Maximum XOR sum

Given a non empty array, the elements in the array are A0, A1, A2 , an-1, where 0 ≤ AI < 2 ^ 31

Find the XOR (XOR) result of AI and AJ, where 0 ≤ I, J < n.

You can do it in o(n)Time to solve this problem?

Example:

Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation:The biggest result is5 ^ 25 = 28.

Initial ideas

The law of violence is simple, but… Can think of, and it’s very complicated

Correct thinking

XOR means that if the corresponding bit is the same, then the operation result of the two is 0, and the difference is 1

  • 1 ^ 1 = 0 ^ 0 = 0
  • 0 ^ 1 = 1 ^ 0 = 1

That is to say, if you want the XOR number to be large, then you should try to keep two digitsdifferent!In this way, the result will be larger if the XOR of the corresponding bit is 1!

So when is the biggest?

Try to make high XOR 1 to keep the maximum!!!Prefix tree of algorithm -- Maximum XOR sum Prefix tree of algorithm -- Maximum XOR sum Prefix tree of algorithm -- Maximum XOR sum

Why? Because there are1 * * * *Must be greater than0 * * * *nature

That is, no matter how the low bit is, as long as the high bit is 1, it must be larger than the high position which is not 1. Therefore, in the traversal process, you only need to find the one that is different from the current bit, so that the XOR result is 1! If not, there will be no effect.

In this process, you can usetrie To save the situation of each number!

Try to find the node which is different from the current bit in the high positiongreedy

Prefix tree of algorithm -- Maximum XOR sum

Implementation method: prefix tree + greedy

package ddx.December.day6;

public class Normal_421 {
    public int maxXOR(int[] nums) {
        BitTrieNode root = buildBitTrie(nums);
        return searchMaXOR(nums,root);
    }
    //Prefix tree + greed!
    public int searchMaXOR(int[] nums, BitTrieNode root){
        int max = -1; //Maximum record
        for(int num : nums){//Traversing the array, find each number and which number XOR result is the largest!
            BitTrieNode curNode = root;
            for(int index = 31;index >= 0;index--){
                int curBit = num & (1 << index); //Get the highest number
                if(curBit == 0){ //If the highest bit is 0, then I need one in the prefix tree
                    curNode = curNode.one != null ? curNode.one:curNode.zero; //Try to find the node opposite to the current bit!!!!!
                }else{ //And vice versa
                    curNode = curNode.zero != null ? curNode.zero:curNode.one; //Try to find the node opposite to the current bit!!!!!
                }
            }
            max = Math.max(max,num ^ curNode.val); //Update maximum value! here curode.val It's the number that is exclusive or the largest with num!!!
        }
        return max;
    }

    public BitTrieNode buildBitTrie(int[] nums) { //Create bit prefix tree
        BitTrieNode root = new BitTrieNode(-1); //Initialize root node
        for (int num : nums) {
            BitTrieNode curNode = root;
            for (int index = 31; index >= 0; index--) { //Each number can not exceed 32 bits at most! Create all the 32 bits
                int curBit = num & (1 << index); //Get the current position
                if (curBit == 0) {
                    if (curNode.zero == null) { //If not, create
                        curNode.zero = new BitTrieNode(0);
                    }
                    curNode = curNode.zero;
                } else {
                    if (curNode.one == null) {//If not, create
                        curNode.one = new BitTrieNode(1);
                    }
                    curNode = curNode.one;
                }
            }
            curNode.val = num; //Assignment
        }
        return root;
    }

}

class BitTrieNode {
    public BitTrieNode zero;
    public BitTrieNode one;
    public int val;

    BitTrieNode(int val) {
        this.val = val;
    }
}

Output results

And3The maximum number of XOR results is:25The exclusive or result is:26And10The maximum number of XOR results is:25The exclusive or result is:19And5The maximum number of XOR results is:25The exclusive or result is:28And25The maximum number of XOR results is:5The exclusive or result is:28And2The maximum number of XOR results is:25The exclusive or result is:27And8The maximum number of XOR results is:25The exclusive or result is:17

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint