Maximum XOR Answer After Each Removal

You are given a sorted array nums of n non-negative integers and an integer maximumBit. You have to perform the following query n times:

For each query, do the following:

  • Consider the current array.
  • Find a non-negative integer k such that 0 <= k < 2^maximumBit.
  • Choose k so that the value: nums[0] XOR nums[1] XOR ... XOR nums[m-1] XOR k is as large as possible, where m is the current size of the array.
  • The value of k found in this step is the answer for the current query.
  • After that, remove the last element from the current array.

Return an array answer of size n, where answer[i] is the value of k chosen for the ith query.

Example 1:

Input: nums = [1, 2, 3], maximumBit = 2

Output: [3, 0, 2]

Explanation:

Maximum value using 2 bits is 3.

  • XOR of [1,2,3] is 0 → k = 3
  • XOR of [1,2] is 3 → k = 0
  • XOR of [1] is 1 → k = 2

Example 2:

Input: nums = [0, 2, 5, 7], maximumBit = 3

Output: [7, 0, 5, 7]

Explanation:

Maximum value using 3 bits is 7.

  • XOR of [0,2,5,7] is 0 → k = 7
  • XOR of [0,2,5] is 7 → k = 0
  • XOR of [0,2] is 2 → k = 5
  • XOR of [0] is 0 → k = 7

Now Your Turn!

Pick the correct output for the given input

Input: nums = [4, 5], maximumBit = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • nums.length == n
  • 1 <= n <= 105
  • 1 <= maximumBit <= 20
  • 0 <= nums[i] < 2^maximumBit
  • nums is sorted in non-decreasing order

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
// Your code goes here
}
};
Test Case

Input:

Maximum Bit
Nums