{"id":9766,"date":"2022-06-25T20:58:32","date_gmt":"2022-06-26T03:58:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9766"},"modified":"2022-06-27T22:46:54","modified_gmt":"2022-06-28T05:46:54","slug":"leetcode-2317-maximum-xor-after-operations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-2317-maximum-xor-after-operations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2317. Maximum XOR After Operations"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 2317. Maximum XOR After Operations - \u5237\u9898\u627e\u5de5\u4f5c EP398\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/8wexnTx5wlE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given a\u00a0<strong>0-indexed<\/strong>\u00a0integer array\u00a0<code>nums<\/code>. In one operation, select\u00a0<strong>any<\/strong>\u00a0non-negative integer\u00a0<code>x<\/code>\u00a0and an index\u00a0<code>i<\/code>, then\u00a0<strong>update<\/strong>\u00a0<code>nums[i]<\/code>\u00a0to be equal to\u00a0<code>nums[i] AND (nums[i] XOR x)<\/code>.<\/p>\n\n\n\n<p>Note that&nbsp;<code>AND<\/code>&nbsp;is the bitwise AND operation and&nbsp;<code>XOR<\/code>&nbsp;is the bitwise XOR operation.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;possible bitwise XOR of all elements of&nbsp;<\/em><code>nums<\/code><em>&nbsp;after applying the operation&nbsp;<strong>any number<\/strong>&nbsp;of times<\/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 = [3,2,4,6]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.\nNow, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.\nIt can be shown that 7 is the maximum possible bitwise XOR.\nNote that other operations may be used to achieve a bitwise XOR of 7.<\/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 = [1,2,3,9,2]\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong> Apply the operation zero times.\nThe bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.\nIt can be shown that 11 is the maximum possible bitwise XOR.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>8<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bitwise OR<\/strong><\/h2>\n\n\n\n<p>The maximum possible number MAX = nums[0] | nums[1] | &#8230; | nums[n &#8211; 1]. <\/p>\n\n\n\n<p>We need to prove:<br>1) MAX is achievable.<br>2) MAX is the largest number we can get.<\/p>\n\n\n\n<p><code>nums[i] AND (nums[i] XOR x)<\/code> means that we can turn any 1 bits to 0 for nums[i].<\/p>\n\n\n\n<p>1) If the i-th bit of MAX is 1, which means there are at least one number with i-th bit equals to 1, however, for XOR, if there are even numbers with i-th bit equal to one, the final results will be 0 for i-th bit, we get a smaller number. By using the operation, we can choose one of them and flip the bit.<br><br>**1** XOR <meta charset=\"utf-8\">**1** XOR <meta charset=\"utf-8\">**1** XOR <meta charset=\"utf-8\">**1** = **0** =&gt; <br>**<strong><span class=\"has-inline-color has-vivid-red-color\">0<\/span><\/strong>** XOR <meta charset=\"utf-8\">**1** XOR <meta charset=\"utf-8\">**1** XOR <meta charset=\"utf-8\">**1** = **<strong><span class=\"has-inline-color has-vivid-red-color\">1<\/span><\/strong>**<br><\/p>\n\n\n\n<p>2) If the i-th bit of MAX is 0, which means the i-th bit of all the numbers is 0, there is nothing we can do with the operation, and the XOR will be 0 as well.<br>e.g. <meta charset=\"utf-8\">**0** XOR <meta charset=\"utf-8\">**0** XOR <meta charset=\"utf-8\">**0** XOR <meta charset=\"utf-8\">**0** = **0**<\/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  int maximumXOR(vector<int>& nums) {\n    return reduce(begin(nums), end(nums), 0, bit_or());\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a\u00a00-indexed\u00a0integer array\u00a0nums. In one operation, select\u00a0any\u00a0non-negative integer\u00a0x\u00a0and an index\u00a0i, then\u00a0update\u00a0nums[i]\u00a0to be equal to\u00a0nums[i] AND (nums[i] XOR x). Note that&nbsp;AND&nbsp;is the bitwise AND&#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,63],"class_list":["post-9766","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-medium","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9766","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=9766"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9766\/revisions"}],"predecessor-version":[{"id":9772,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9766\/revisions\/9772"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}