Maximum XOR for each query

Jyothi
2 min readMay 12, 2024

--

Problem:

https://leetcode.com/problems/maximum-xor-for-each-query/description/

Aim is to compute the max k value for each query. To compute the maximum k value, we can follow these steps:

1. Compute in Reverse Order: The XOR operation is performed by considering only the first element nums[0] for the last query. For the second last query, the XOR operation is performed by using the first two elements nums[0], nums[1]. This pattern suggests computing the queries in reverse order, starting from the nth query down to the first query. This simplifies the computation process.

2. Maintain the running XOR: We can maintain a variable called totXor. This value is updated with the XOR of current element nums[i] in each iteration.

3. Find the max k value: The condition is that the XOR of i elements and k should have the maximum value. To find such k value, we can perform XOR between totXor value and the maximum value. This maximum value is pow(2, maxBit) -1.

Below is the code to find k values:

--

--