{"id":8361,"date":"2021-04-17T12:08:24","date_gmt":"2021-04-17T19:08:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8361"},"modified":"2021-04-17T12:32:14","modified_gmt":"2021-04-17T19:32:14","slug":"leetcode-1829-maximum-xor-for-each-query","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1829-maximum-xor-for-each-query\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1829. Maximum XOR for Each Query"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>sorted<\/strong>&nbsp;array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<code>n<\/code>&nbsp;non-negative integers and an integer&nbsp;<code>maximumBit<\/code>. You want to perform the following query&nbsp;<code>n<\/code>&nbsp;<strong>times<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Find a non-negative integer&nbsp;<code>k &lt; 2<sup>maximumBit<\/sup><\/code>&nbsp;such that&nbsp;<code>nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k<\/code>&nbsp;is&nbsp;<strong>maximized<\/strong>.&nbsp;<code>k<\/code>&nbsp;is the answer to the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;query.<\/li><li>Remove the&nbsp;<strong>last&nbsp;<\/strong>element from the current array&nbsp;<code>nums<\/code>.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>an array<\/em>&nbsp;<code>answer<\/code><em>, where&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is the answer to the&nbsp;<\/em><code>i<sup>th<\/sup><\/code><em>&nbsp;query<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,1,3], maximumBit = 2\n<strong>Output:<\/strong> [0,3,2,3]\n<strong>Explanation<\/strong>: The queries are answered as follows:\n1<sup>st<\/sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.\n2<sup>nd<\/sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.\n3<sup>rd<\/sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.\n4<sup>th<\/sup> query: nums = [0], k = 3 since 0 XOR 3 = 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,4,7], maximumBit = 3\n<strong>Output:<\/strong> [5,2,6,5]\n<strong>Explanation<\/strong>: The queries are answered as follows:\n1<sup>st<\/sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.\n2<sup>nd<\/sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.\n3<sup>rd<\/sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.\n4<sup>th<\/sup> query: nums = [2], k = 5 since 2 XOR 5 = 7.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [0,1,2,2,5,7], maximumBit = 3\n<strong>Output:<\/strong> [4,3,6,4,6,7]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= maximumBit &lt;= 20<\/code><\/li><li><code>0 &lt;= nums[i] &lt; 2<sup>maximumBit<\/sup><\/code><\/li><li><code>nums<\/code>\u200b\u200b\u200b is sorted in&nbsp;<strong>ascending<\/strong>&nbsp;order.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix XOR<\/strong><\/h2>\n\n\n\n<p>Compute s = nums[0] ^ nums[1] ^ &#8230; nums[n-1] first<\/p>\n\n\n\n<p>to remove nums[i], we just need to do s ^= nums[i]<\/p>\n\n\n\n<p>We can always maximize the xor of s and k to (2^maxbit &#8211; 1)<br>k = (2 ^ maxbit &#8211; 1) ^ s<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {\n    const int t = (1 << maximumBit) - 1;\n    const int n = nums.size();\n    vector<int> ans(n);\n    int s = 0;\n    for (int x : nums) s ^= x;    \n    for (int i = n - 1; i >= 0; --i) {\n      ans[n - i - 1] = t ^ s;\n      s ^= nums[i];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;sorted&nbsp;array&nbsp;nums&nbsp;of&nbsp;n&nbsp;non-negative integers and an integer&nbsp;maximumBit. You want to perform the following query&nbsp;n&nbsp;times: Find a non-negative integer&nbsp;k &lt; 2maximumBit&nbsp;such that&nbsp;nums[0] XOR nums[1] XOR&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,177,97,63],"class_list":["post-8361","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-medium","tag-prefix","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8361"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8361\/revisions"}],"predecessor-version":[{"id":8365,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8361\/revisions\/8365"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}