Problem solution · Python

Maximum Subarray Xor with Bounded Range

Maximum Subarray Xor with Bounded Range: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
200 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

Breadth-first search

For Maximum Subarray Xor with Bounded Range, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 200 lines of Python from the credited upstream file maximum-subarray-xor-with-bounded-range.py.
  • The implementation visibly relies on sequence storage, hash lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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 codeMaximum Subarray Xor with Bounded Range · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogr), r = max(max(nums), 1)# Space: O(n) import collections  # two pointers, mono deque, bitmasks, prefix sum, hash tableclass Solution(object):    def maxXor(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: int        """        lookup = [-1]*len(nums)        max_dq = collections.deque()        min_dq = collections.deque()        left = 0        for right in xrange(len(nums)):            while max_dq and nums[max_dq[-1]] <= nums[right]:                max_dq.pop()            max_dq.append(right)            while min_dq and nums[min_dq[-1]] >= nums[right]:                min_dq.pop()            min_dq.append(right)            while nums[max_dq[0]]-nums[min_dq[0]] > k:                if max_dq and max_dq[0] == left:                    max_dq.popleft()                if min_dq and min_dq[0] == left:                    min_dq.popleft()                left += 1            lookup[right] = left        result = 0        mx = max(max(nums), 1)        for i in reversed(xrange(mx.bit_length())):            lookup2 = collections.defaultdict(int)            lookup2[0] = prefix = 0            for right in xrange(len(nums)):                prefix ^= nums[right]>>i                if ((result>>i)|1)^prefix in lookup2 and lookup2[((result>>i)|1)^prefix] >= lookup[right]:                    result |= 1<<i                    break                lookup2[prefix] = right+1        return result  # Time:  O(nlogr), r = max(max(nums), 1)# Space: O(n + t)import collections  # two pointers, mono deque, bitmasks, prefix sum, trieclass Solution2(object):    def maxXor(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: int        """        class Trie(object):            def __init__(self, bit_length):                self.__lefts = [-1]*(1+(1+len(nums))*bit_length)  # preallocate to speed up performance                self.__rights = [-1]*(1+(1+len(nums))*bit_length)                self.__cnts = [0]*(1+(1+len(nums))*bit_length)                self.__i = 0                self.__new_node()                self.__bit_length = bit_length                        def __new_node(self):                self.__i += 1                return self.__i-1             def add(self, num, diff):                curr = 0                for i in reversed(xrange(self.__bit_length)):                    x = (num>>i)&1                    if x == 0:                        if self.__lefts[curr] == -1:                            self.__lefts[curr] = self.__new_node()                        curr = self.__lefts[curr]                    else:                        if self.__rights[curr] == -1:                            self.__rights[curr] = self.__new_node()                        curr = self.__rights[curr]                    self.__cnts[curr] += diff                                    def query(self, prefix):                result = curr = 0                for i in reversed(xrange(self.__bit_length)):                    x = (prefix>>i)&1                    l, r = (self.__lefts, self.__rights) if x^1 else (self.__rights, self.__lefts)                    if r[curr] != -1 and self.__cnts[r[curr]]:                        result |= 1<<i                        curr = r[curr]                    else:                        curr = l[curr]                return result            result = 0        prefix = [0]*(len(nums)+1)        for i in xrange(len(nums)):            prefix[i+1] = prefix[i]^nums[i]        mx = max(max(nums), 1)        trie = Trie(mx.bit_length())        trie.add(prefix[0], +1)        max_dq = collections.deque()        min_dq = collections.deque()        left = 0        for right in xrange(len(nums)):            while max_dq and nums[max_dq[-1]] <= nums[right]:                max_dq.pop()            max_dq.append(right)            while min_dq and nums[min_dq[-1]] >= nums[right]:                min_dq.pop()            min_dq.append(right)            while nums[max_dq[0]]-nums[min_dq[0]] > k:                trie.add(prefix[left], -1)                if max_dq and max_dq[0] == left:                    max_dq.popleft()                if min_dq and min_dq[0] == left:                    min_dq.popleft()                left += 1            result = max(result, trie.query(prefix[right+1]))            trie.add(prefix[right+1], +1)        return result  # Time:  O(nlogr), r = max(max(nums), 1)# Space: O(n + t)import collections  # two pointers, mono deque, bitmasks, prefix sum, trieclass Solution3(object):    def maxXor(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: int        """        class Trie(object):            def __init__(self, bit_length):                self.__nodes = []                self.__cnts = []                self.__new_node()                self.__bit_length = bit_length                        def __new_node(self):                self.__nodes.append([-1]*2)                self.__cnts.append(0)                return len(self.__nodes)-1             def add(self, num, diff):                curr = 0                for i in reversed(xrange(self.__bit_length)):                    x = (num>>i)&1                    if self.__nodes[curr][x] == -1:                        self.__nodes[curr][x] = self.__new_node()                    curr = self.__nodes[curr][x]                    self.__cnts[curr] += diff                                    def query(self, prefix):                result = curr = 0                for i in reversed(xrange(self.__bit_length)):                    x = (prefix>>i)&1                    if self.__nodes[curr][x^1] != -1 and self.__cnts[self.__nodes[curr][x^1]]:                        result |= 1<<i                        curr = self.__nodes[curr][x^1]                    else:                        curr = self.__nodes[curr][x]                return result            result = 0        prefix = [0]*(len(nums)+1)        for i in xrange(len(nums)):            prefix[i+1] = prefix[i]^nums[i]        mx = max(max(nums), 1)        trie = Trie(mx.bit_length())        trie.add(prefix[0], +1)        max_dq = collections.deque()        min_dq = collections.deque()        left = 0        for right in xrange(len(nums)):            while max_dq and nums[max_dq[-1]] <= nums[right]:                max_dq.pop()            max_dq.append(right)            while min_dq and nums[min_dq[-1]] >= nums[right]:                min_dq.pop()            min_dq.append(right)            while nums[max_dq[0]]-nums[min_dq[0]] > k:                trie.add(prefix[left], -1)                if max_dq and max_dq[0] == left:                    max_dq.popleft()                if min_dq and min_dq[0] == left:                    min_dq.popleft()                left += 1            result = max(result, trie.query(prefix[right+1]))            trie.add(prefix[right+1], +1)        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 ↗