|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-xor-for-each-query/">1829. Maximum XOR for Each Query</a></h2><h3>Medium</h3><hr><p>You are given a <strong>sorted</strong> array <code>nums</code> of <code>n</code> non-negative integers and an integer <code>maximumBit</code>. You want to perform the following query <code>n</code> <strong>times</strong>:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li>Find a non-negative integer <code>k < 2<sup>maximumBit</sup></code> such that <code>nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k</code> is <strong>maximized</strong>. <code>k</code> is the answer to the <code>i<sup>th</sup></code> query.</li> |
| 5 | + <li>Remove the <strong>last </strong>element from the current array <code>nums</code>.</li> |
| 6 | +</ol> |
| 7 | + |
| 8 | +<p>Return <em>an array</em> <code>answer</code><em>, where </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query</em>.</p> |
| 9 | + |
| 10 | +<p> </p> |
| 11 | +<p><strong class="example">Example 1:</strong></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | +<strong>Input:</strong> nums = [0,1,1,3], maximumBit = 2 |
| 15 | +<strong>Output:</strong> [0,3,2,3] |
| 16 | +<strong>Explanation</strong>: The queries are answered as follows: |
| 17 | +1<sup>st</sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3. |
| 18 | +2<sup>nd</sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3. |
| 19 | +3<sup>rd</sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3. |
| 20 | +4<sup>th</sup> query: nums = [0], k = 3 since 0 XOR 3 = 3. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> nums = [2,3,4,7], maximumBit = 3 |
| 27 | +<strong>Output:</strong> [5,2,6,5] |
| 28 | +<strong>Explanation</strong>: The queries are answered as follows: |
| 29 | +1<sup>st</sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7. |
| 30 | +2<sup>nd</sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7. |
| 31 | +3<sup>rd</sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7. |
| 32 | +4<sup>th</sup> query: nums = [2], k = 5 since 2 XOR 5 = 7. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> nums = [0,1,2,2,5,7], maximumBit = 3 |
| 39 | +<strong>Output:</strong> [4,3,6,4,6,7] |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>nums.length == n</code></li> |
| 47 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>1 <= maximumBit <= 20</code></li> |
| 49 | + <li><code>0 <= nums[i] < 2<sup>maximumBit</sup></code></li> |
| 50 | + <li><code>nums</code> is sorted in <strong>ascending</strong> order.</li> |
| 51 | +</ul> |
0 commit comments