{"id":9984,"date":"2023-03-11T21:02:21","date_gmt":"2023-03-12T05:02:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9984"},"modified":"2023-03-11T21:03:18","modified_gmt":"2023-03-12T05:03:18","slug":"leetcode-2588-count-the-number-of-beautiful-subarrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2588-count-the-number-of-beautiful-subarrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2588. Count the Number of Beautiful Subarrays"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>. In one operation, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose two different indices&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>j<\/code>&nbsp;such that&nbsp;<code>0 &lt;= i, j &lt; nums.length<\/code>.<\/li><li>Choose a non-negative integer&nbsp;<code>k<\/code>&nbsp;such that the&nbsp;<code>k<sup>th<\/sup><\/code>&nbsp;bit (<strong>0-indexed<\/strong>) in the binary representation of&nbsp;<code>nums[i]<\/code>&nbsp;and&nbsp;<code>nums[j]<\/code>&nbsp;is&nbsp;<code>1<\/code>.<\/li><li>Subtract&nbsp;<code>2<sup>k<\/sup><\/code>&nbsp;from&nbsp;<code>nums[i]<\/code>&nbsp;and&nbsp;<code>nums[j]<\/code>.<\/li><\/ul>\n\n\n\n<p>A subarray is&nbsp;<strong>beautiful<\/strong>&nbsp;if it is possible to make all of its elements equal to&nbsp;<code>0<\/code>&nbsp;after applying the above operation any number of times.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<strong>beautiful subarrays<\/strong>&nbsp;in the array<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>A subarray is a contiguous&nbsp;<strong>non-empty<\/strong>&nbsp;sequence of elements within an array.<\/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 = [4,3,1,2,4]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 2 beautiful subarrays in nums: [4,3,1,2,4] and [4,3,1,2,4].\n- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:\n  - Choose [3, 1, 2] and k = 1. Subtract 2<sup>1<\/sup> from both numbers. The subarray becomes [1, 1, 0].\n  - Choose [1, 1, 0] and k = 0. Subtract 2<sup>0<\/sup> from both numbers. The subarray becomes [0, 0, 0].\n- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:\n  - Choose [4, 3, 1, 2, 4] and k = 2. Subtract 2<sup>2<\/sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0].\n  - Choose [0, 3, 1, 2, 0] and k = 0. Subtract 2<sup>0<\/sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0].\n  - Choose [0, 2, 0, 2, 0] and k = 1. Subtract 2<sup>1<\/sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0].\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 = [1,10,4]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no beautiful subarrays in nums.\n<\/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>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable + Prefix XOR<\/strong><\/h2>\n\n\n\n<p>The problem is asking to find # of subarrays whose element wise xor is 0. We can use a hashtable to store the frequency of each prefix xor value, which reduces this problem to # of Subarray sum equal to k.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  long long beautifulSubarrays(vector<int>& nums) {\n    long long ans = 0;\n    unordered_map<int, int> m{{0, 1}};\n    int x = 0;\n    for (int i = 0; i < nums.size(); ++i) {\n      x ^= nums[i];\n      ans += m[x];\n      m[x] += 1;\n    }\n    return ans;\n  } \n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums. In one operation, you can: Choose two different indices&nbsp;i&nbsp;and&nbsp;j&nbsp;such that&nbsp;0 &lt;= i, j &lt; nums.length. Choose a non-negative integer&nbsp;k&nbsp;such that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,791,63],"class_list":["post-9984","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-prefix-xor","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9984","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=9984"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9984\/revisions"}],"predecessor-version":[{"id":9987,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9984\/revisions\/9987"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}