Watch 10 video solutions for Maximum XOR for Each Query, a medium level problem involving Array, Bit Manipulation, Prefix Sum. This walkthrough by codestorywithMIK has 9,107 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a sorted array nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times:
k < 2maximumBit such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the ith query.nums.Return an array answer, where answer[i] is the answer to the ith query.
Example 1:
Input: nums = [0,1,1,3], maximumBit = 2 Output: [0,3,2,3] Explanation: The queries are answered as follows: 1st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3. 2nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3. 3rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3. 4th query: nums = [0], k = 3 since 0 XOR 3 = 3.
Example 2:
Input: nums = [2,3,4,7], maximumBit = 3 Output: [5,2,6,5] Explanation: The queries are answered as follows: 1st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7. 2nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7. 3rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7. 4th query: nums = [2], k = 5 since 2 XOR 5 = 7.
Example 3:
Input: nums = [0,1,2,2,5,7], maximumBit = 3 Output: [4,3,6,4,6,7]
Constraints:
nums.length == n1 <= n <= 1051 <= maximumBit <= 200 <= nums[i] < 2maximumBitnumsāāā is sorted in ascending order.Problem Overview: You receive an integer array nums and a value maximumBit. For each query, you consider the prefix of the array and choose an integer k (0 ⤠k < 2maximumBit) that maximizes prefixXor ^ k. After each query, the last element of the array is removed. The task is to return the optimal k for every query.
Approach 1: Precompute and Use XOR Properties (Time: O(n), Space: O(n))
This approach relies on a key property of bit manipulation: the value that maximizes x ^ k is the bitwise complement of x within the allowed bit range. First compute the prefix XOR of the entire array using a prefix XOR technique. Let mask = (1 << maximumBit) - 1, which represents the largest number allowed for k. For each query, the best value of k is simply prefixXor ^ mask. After producing the result, remove the last element logically by XOR-ing it out of the running prefix. This avoids recomputing XOR values repeatedly and processes the queries in reverse order.
The strength of this method is its simplicity. You compute the total XOR once, then update it incrementally as the array shrinks. XOR operations are constant time, so the entire algorithm runs in linear time relative to the array size.
Approach 2: Calculate On-The-Fly with Prefix XOR Array (Time: O(n), Space: O(n))
This variation explicitly builds a prefix XOR array where prefix[i] stores the XOR of elements from index 0 to i. Each query corresponds to a prefix length that decreases over time. Instead of modifying a running XOR variable, you directly reference the appropriate prefix value. For the current prefix XOR value x, compute the optimal answer using the same mask trick: k = x ^ mask. Because XOR with the mask flips all bits within the allowed range, the result produces the maximum possible XOR value.
The main difference is clarity versus memory usage. Using a prefix array can make debugging easier because every prefix XOR is stored explicitly. However, it requires extra memory compared with maintaining a single running XOR.
Recommended for interviews: The expected solution uses XOR properties with a running prefix value and a bitmask. Interviewers want to see recognition that maximizing x ^ k means flipping every bit within the allowed range. Starting with the straightforward prefix XOR idea demonstrates understanding, while deriving the mask trick shows strong comfort with bitwise operations. The optimal implementation runs in O(n) time with O(1) auxiliary space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force XOR for Each Query | O(n^2) | O(1) | Useful only for understanding the problem constraints or validating small test cases |
| Precompute and Use XOR Properties | O(n) | O(1) | Best general solution; uses XOR mask trick and updates prefix XOR as elements are removed |
| Prefix XOR Array | O(n) | O(n) | When you want explicit prefix values for clarity or debugging |