Problem solution · Python

Partition Array for Maximum Xor and and

Partition Array for Maximum Xor and and: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Direct simulation
Source
Kamyu LeetCode Solutions
Length
85 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Partition Array for Maximum Xor and and, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 85 lines of Python from the credited upstream file partition-array-for-maximum-xor-and-and.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codePartition Array for Maximum Xor and and · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogr * 2^n)# Space: O(2^n) # bitmasks, greedyclass Solution(object):    def maximizeXorAndXor(self, nums):        """        :type nums: List[int]        :rtype: int        """        def max_xor_subset(nums):  # Time: O(nlogr)            base = [0]*l             for x in nums:  # gaussian elimination over GF(2)                for i in reversed(xrange(len(base))):                    if not x&(1<<i):                        continue                    if base[i] == 0:                        base[i] = x                        break                    x ^= base[i]            max_xor = 0            for b in reversed(base):  # greedy                if (max_xor^b) > max_xor:                    max_xor ^= b            return max_xor         l = max(nums).bit_length()        n = len(nums)        and_arr = [0]*(1<<n)        xor_arr = [0]*(1<<n)        for mask in xrange(1, 1<<n):            lb = mask&-mask            i = lb.bit_length()-1            and_arr[mask] = and_arr[mask^lb]&nums[i] if mask^lb else nums[i]            xor_arr[mask] = xor_arr[mask^lb]^nums[i]        result = 0        full_mask = (1<<n)-1        for mask in xrange(1, 1<<n):            total_and = and_arr[mask]            total_xor = xor_arr[full_mask^mask]            max_xor = max_xor_subset(((nums[i]&~total_xor) for i in xrange(n) if not (mask&(1<<i))))            result = max(result, total_and+total_xor+2*max_xor)        return result  # Time:  O(nlogr * 2^n)# Space: O(1)# bitmasks, greedyclass Solution2(object):    def maximizeXorAndXor(self, nums):        """        :type nums: List[int]        :rtype: int        """        def max_xor_subset(nums):  # Time: O(nlogr)            base = [0]*l             for x in nums:  # gaussian elimination over GF(2)                for i in reversed(xrange(len(base))):                    if not x&(1<<i):                        continue                    if base[i] == 0:                        base[i] = x                        break                    x ^= base[i]            max_xor = 0            for b in reversed(base):  # greedy                if (max_xor^b) > max_xor:                    max_xor ^= b            return max_xor         l = max(nums).bit_length()        n = len(nums)        result = 0        for mask in xrange(1, 1<<n):            and_arr = -1            xor_arr = 0            for i in xrange(n):                if mask&(1<<i):                    and_arr = and_arr&nums[i] if and_arr != -1 else nums[i]                else:                    xor_arr ^= nums[i]            max_xor = max_xor_subset(((nums[i]&~xor_arr) for i in xrange(n) if not (mask&(1<<i))))            result = max(result, and_arr+xor_arr+2*max_xor)        return result 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗