Solutions for Maximum XOR

    • @GarretVam
    • @mason, submitted in Python 2, achived 100% score
    INT_BITS = 32
    
    
    def solve(set):
        n=len(set)
        index = 0
        for i in range(INT_BITS - 1, -1, -1):
            maxInd = index
            maxEle = -2147483648
            for j in range(index, n):
                if set[j] & 1 << i != 0 and set[j] > maxEle:
                    maxEle = set[j]
                    maxInd = j
            if maxEle == -2147483648:
                continue
            temp = set[index]
            set[index] = set[maxInd]
            set[maxInd] = temp
            maxInd = index
            for j in range(n):
                if j != maxInd and set[j] & 1 << i != 0:
                    set[j] = set[j] ^ set[maxInd]
            index = index + 1
        res = 0
        for i in range(n):
            res = res ^ set[i]
        return res
    
    
    • @GarretVam
    • @mason, submitted in Python 3, achived 100% score
    INT_BITS = 32
    
    
    def solve(set):
        n=len(set)
        index = 0
        for i in range(INT_BITS - 1, -1, -1):
            maxInd = index
            maxEle = -2147483648
            for j in range(index, n):
                if set[j] & 1 << i != 0 and set[j] > maxEle:
                    maxEle = set[j]
                    maxInd = j
            if maxEle == -2147483648:
                continue
            temp = set[index]
            set[index] = set[maxInd]
            set[maxInd] = temp
            maxInd = index
            for j in range(n):
                if j != maxInd and set[j] & 1 << i != 0:
                    set[j] = set[j] ^ set[maxInd]
            index = index + 1
        res = 0
        for i in range(n):
            res = res ^ set[i]
        return res