Skip to content
LC-2939 Medium LeetCode

2939. Maximum Xor Product

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 27% Topics: Math, Greedy, Bit Manipulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# greedy
class Solution(object):
    def maximumXorProduct(self, a, b, n):
        """
        :type a: int
        :type b: int
        :type n: int
        :rtype: int
        """
        MOD = 10**9+7
        for i in reversed(xrange(n)):
            base = 1<<i
            if min(a, b)&base == 0:
                a, b = a^base, b^base
        return (a%MOD)*(b%MOD)%MOD

Solution from kamyu104/LeetCode-Solutions · MIT