1829. Maximum XOR for Each Query
Difficulty: Medium
Topics: Array
, Bit Manipulation
, Prefix Sum
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:
- Find a non-negative integer
k < 2maximumBit
such thatnums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k
is maximized.k
is the answer to theith
query. - Remove the last element from the current array
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:
- 1stst 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:
- 1stst 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 == n
1 <= n <= 105
1 <= maximumBit <= 20
0 <= nums[i] < 2maximumBit
-
nums
is sorted in ascending order.
Hint:
- Note that the maximum possible XOR result is always
2(maximumBit) - 1
- So the answer for a prefix is the XOR of that prefix XORed with
2(maximumBit)-1
Solution:
We need to efficiently calculate the XOR of elements in the array and maximize the result using a value k
such that k
is less than 2^maximumBit
. Here's the approach for solving this problem:
Observations and Approach
Maximizing XOR:
The maximum number we can XOR with any prefix sum formaximumBit
bits is ( 2^{\text{maximumBit}} - 1 ). This is because XORing with a number of all1
s (i.e.,111...1
in binary) will always maximize the result.Prefix XOR Calculation:
Instead of recalculating the XOR for each query, we can maintain a cumulative XOR for the entire array. Since XOR has the property thatA XOR B XOR B = A
, removing the last element from the array can be achieved by XORing out that element from the cumulative XOR.-
Algorithm:
- Compute the XOR of all elements in
nums
initially. Let's call thiscurrentXOR
. - For each query (from last to first):
- Calculate the optimal value of
k
for that query by XORingcurrentXOR
withmaxNum
wheremaxNum = 2^maximumBit - 1
. - Append
k
to the result list. - Remove the last element from
nums
by XORing it out ofcurrentXOR
.
- Calculate the optimal value of
- The result list will contain the answers in reverse order, so reverse it at the end.
- Compute the XOR of all elements in
Let's implement this solution in PHP: 1829. Maximum XOR for Each Query
<?php
/**
* @param Integer[] $nums
* @param Integer $maximumBit
* @return Integer[]
*/
function getMaximumXor($nums, $maximumBit) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
$nums = [0,1,1,3];
$maximumBit = 2;
print_r(getMaximumXor($nums, $maximumBit)); // Output should be [0, 3, 2, 3]
?>
Explanation:
-
Calculate
maxNum
:-
maxNum
is calculated as2^maximumBit - 1
, which is the number with all1
s in binary for the specified bit length.
-
-
Initial XOR Calculation:
- We XOR all elements in
nums
to get the cumulative XOR (currentXOR
), representing the XOR of all numbers in the array.
- We XOR all elements in
-
Iterate Backwards:
- We start from the last element in
nums
and calculate the maximum XOR for each step:-
currentXOR ^ maxNum
gives the maximumk
for the current state. - Append
k
toanswer
.
-
- We then XOR the last element of
nums
withcurrentXOR
to "remove" it from the XOR sum for the next iteration.
- We start from the last element in
-
Return the Answer:
- Since we processed the list in reverse,
answer
will contain the values in reverse order, so the final list is already arranged correctly for our requirements.
- Since we processed the list in reverse,
Complexity Analysis
- Time Complexity: O(n), since we compute the initial XOR in O(n) and each query is processed in constant time.
-
Space Complexity: O(n), for storing the
answer
.
This code is efficient and should handle the upper limits of the constraints well.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)