java.lang.Object
g1801_1900.s1829_maximum_xor_for_each_query.Solution

public class Solution extends Object
1829 - Maximum XOR for Each Query.<p>Medium</p> <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> <ol> <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> <li>Remove the <strong>last</strong> element from the current array <code>nums</code>.</li> </ol> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [0,1,1,3], maximumBit = 2</p> <p><strong>Output:</strong> [0,3,2,3]</p> <p><strong>Explanation:</strong> The queries are answered as follows:</p> <p>1<sup>st</sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.</p> <p>2<sup>nd</sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.</p> <p>3<sup>rd</sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.</p> <p>4<sup>th</sup> query: nums = [0], k = 3 since 0 XOR 3 = 3.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,3,4,7], maximumBit = 3</p> <p><strong>Output:</strong> [5,2,6,5]</p> <p><strong>Explanation:</strong> The queries are answered as follows:</p> <p>1<sup>st</sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.</p> <p>2<sup>nd</sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.</p> <p>3<sup>rd</sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.</p> <p>4<sup>th</sup> query: nums = [2], k = 5 since 2 XOR 5 = 7.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [0,1,2,2,5,7], maximumBit = 3</p> <p><strong>Output:</strong> [4,3,6,4,6,7]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums.length == n</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= maximumBit <= 20</code></li> <li><code>0 <= nums[i] < 2<sup>maximumBit</sup></code></li> <li><code>nums</code> is sorted in <strong>ascending</strong> order.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • getMaximumXor

      public int[] getMaximumXor(int[] nums, int maximumBit)