Maximum xor among all subsets of an array

后端 未结 2 1648
栀梦
栀梦 2020-12-23 23:42

I have to find maximum value of exclusive xor among the elements of subsets of an array. I have to check every subset of the array and the subset which will yield maximum xo

2条回答
  •  猫巷女王i
    2020-12-24 00:17

    I guess that you're referring to this question.

    Gaussian Elimination is the algorithm description that I would expect from the math site. This is what the algorithm looks like in Python.

    def max_xor(iterable):
        array = list(iterable)  # make it a list so that we can iterate it twice
        if not array:  # special case the empty array to avoid an empty max
            return 0
        x = 0
        while True:
            y = max(array)
            if y == 0:
                return x
            # y has the leading 1 in the array
            x = max(x, x ^ y)
            # eliminate
            array = [min(z, z ^ y) for z in array]
    

提交回复
热议问题