Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum XOR With an Element From Array

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1707" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi]. The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1. Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the ith query. Example 1: Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]] Output: [3,3,7] Explanation: 1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3. 2) 1 XOR 2 = 3. 3) 5 XOR 2 = 7. Example 2: Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]] Output: [15,-1,5] Constraints: 1 <= nums.length, queries.length <= 105 queries[i].length == 2 0 <= nums[j], xi, mi <= 109

Explanation

Here's a breakdown of the approach, complexity, and the Python code:

  • High-Level Approach:

    • Sort the nums array to efficiently find elements within the mi limit.
    • Use a Trie data structure to store the nums elements encountered so far (that are less than or equal to mi). The Trie will be structured based on the bit representation of the numbers.
    • For each query, traverse the Trie to find the number that maximizes the XOR value with xi, while only considering numbers less than or equal to mi.
  • Complexity:

    • Runtime: O(n log n + q log n), where n is the length of nums and q is the length of queries. Sorting nums takes O(n log n). Inserting and querying the Trie takes O(log n) for each element in nums and each query in queries.
    • Storage: O(n), primarily due to the Trie which can potentially store all elements of nums.
  • Python Code:

Code

    class TrieNode:
    def __init__(self):
        self.children = [None] * 2

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, num):
        node = self.root
        for i in range(31, -1, -1):
            bit = (num >> i) & 1
            if not node.children[bit]:
                node.children[bit] = TrieNode()
            node = node.children[bit]

    def max_xor(self, num):
        node = self.root
        max_xor = 0
        for i in range(31, -1, -1):
            bit = (num >> i) & 1
            opposite_bit = 1 - bit
            if node.children[opposite_bit]:
                max_xor |= (1 << i)
                node = node.children[opposite_bit]
            else:
                node = node.children[bit]
        return max_xor

def maximize_xor(nums, queries):
    nums.sort()
    trie = Trie()
    result = []
    j = 0
    for x, m in queries:
        while j < len(nums) and nums[j] <= m:
            trie.insert(nums[j])
            j += 1
        if j == 0 and (len(nums) == 0 or nums[0] > m):
            result.append(-1)
        else:
            if trie.root.children[0] is None and trie.root.children[1] is None:
                result.append(-1)
            else:
                result.append(trie.max_xor(x))
    return result

More from this blog

C

Chatmagic blog

2894 posts