Maximum xor after operations

November 15, 2022

bit-manipulation

Problem URL: Maximum xor after operations

The problem calls for choosing an integer x, selecting an element n of the list, applying the compound operator op(n,x) = (x&n)^n, and taking the bit-intersection of the modified set. Because of the associative and commutative properties of the XOR operator, it does not matter which n we choose in nums.

class Solution:
    def maximumXOR(self, nums: List[int]) -> int:
        res = 0
        for n in nums:
            res |= n      
        return res

Time complexity: O(n)
Space complexity: O(1)

We can do it with one line using reduce.

class Solution:
    def maximumXOR(self, nums: List[int]) -> int:
        return reduce(ior, nums)