{"id":6739,"date":"2020-05-11T18:45:40","date_gmt":"2020-05-12T01:45:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6739"},"modified":"2020-05-12T22:48:35","modified_gmt":"2020-05-13T05:48:35","slug":"leetcode-1442-count-triplets-that-can-form-two-arrays-of-equal-xor","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1442-count-triplets-that-can-form-two-arrays-of-equal-xor\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1442. Count Triplets That Can Form Two Arrays of Equal XOR"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1442. Count Triplets That Can Form Two Arrays of Equal XOR - \u5237\u9898\u627e\u5de5\u4f5c EP325\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/30A0Z2KDvaA?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>Given an array of&nbsp;integers&nbsp;<code>arr<\/code>.<\/p>\n\n\n\n<p>We want to select three indices&nbsp;<code>i<\/code>,&nbsp;<code>j<\/code>&nbsp;and&nbsp;<code>k<\/code>&nbsp;where&nbsp;<code>(0 &lt;= i &lt; j &lt;= k &lt; arr.length)<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s define&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]<\/code><\/li><li><code>b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]<\/code><\/li><\/ul>\n\n\n\n<p>Note that&nbsp;<strong>^<\/strong>&nbsp;denotes the&nbsp;<strong>bitwise-xor<\/strong>&nbsp;operation.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of triplets<\/em>&nbsp;(<code>i<\/code>,&nbsp;<code>j<\/code>&nbsp;and&nbsp;<code>k<\/code>) Where&nbsp;<code>a == b<\/code>.<\/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> arr = [2,3,1,6,7]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)\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> arr = [1,1,1,1,1]\n<strong>Output:<\/strong> 10\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> arr = [2,3]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,3,5,7,9]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [7,11,12,9,5,2,7,17,22]\n<strong>Output:<\/strong> 8\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 300<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^8<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force (TLE)<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^4)<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 countTriplets(vector<int>& arr) {\n    const int n = arr.size();    \n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        for (int k = j; k < n; ++k) {\n          int a = 0;\n          int b = 0;\n          for (int t = i; t < j; ++t)\n            a ^= arr[t];\n          for (int t = j; t <= k; ++t)\n            b ^= arr[t];\n          if (a == b) ++ans;\n        }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Prefix XORs<\/strong><\/h2>\n\n\n\n<p>Let xors[i] = arr[0] ^ arr[1] ^ ... ^ arr[i-1]<br>arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1] = (arr[0] ^ ... ^ arr[j - 1]) ^ (arr[0] ^ ... ^ arr[i-1]) = xors[j] ^ xors[i]<\/p>\n\n\n\n<p>We then can compute a and b in O(1) time.<\/p>\n\n\n\n<p>Time complexity: O(n^3)<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, 232 ms\nclass Solution {\npublic:\n  int countTriplets(vector<int>& arr) {\n    const int n = arr.size();    \n    int ans = 0;\n    vector<int> xors(n + 1);\n    for (int i = 0; i < n; ++i)\n      xors[i + 1] = xors[i] ^ arr[i];\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        for (int k = j; k < n; ++k) {\n          const int a = xors[j] ^ xors[i];\n          const int b = xors[k + 1] ^ xors[j];         \n          if (a == b) ++ans;\n        }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 3: Prefix XORs II<\/strong><\/h2>\n\n\n\n<p>a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]<br>b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]<br>a == b =&gt; a ^ b == 0<br>XORs(i ~ k)  == 0<br>XORS(0 ~ k) ^ XORs(0 ~ i - 1) = 0<\/p>\n\n\n\n<p>Problem =&gt; find all pairs of (i - 1, k) such that xors[k+1] == xors[i]<br>For each pair (i - 1, k), there are k - i positions we can insert j.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<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 countTriplets(vector<int>& arr) {\n    const int n = arr.size();    \n    int ans = 0;\n    vector<int> xors(n + 1);\n    for (int i = 0; i < n; ++i)\n      xors[i + 1] = xors[i] ^ arr[i];\n    for (int i = 0; i < n; ++i)\n      for (int k = i + 1; k < n; ++k)\n        if (xors[k + 1] == xors[i]) \n          ans += k - i;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 3: HashTable<\/strong><\/h2>\n\n\n\n<p>Similar to target sum, use a hashtable to store the frequency of each prefix xors.<\/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\n\/\/ Author: Huahua 4 ms\nclass Solution {\npublic:\n  int countTriplets(vector<int>& arr) {\n    const int n = arr.size();    \n    int ans = 0;\n    unordered_map<int, int> freq{{0, 1}};    \n    unordered_map<int, int> sum;\n    int X = 0;\n    for (int i = 0; i < n; ++i) {\n      X ^= arr[i];\n      ans += freq[X] * i - sum[X];\n      ++freq[X];\n      sum[X] += i + 1;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of&nbsp;integers&nbsp;arr. We want to select three indices&nbsp;i,&nbsp;j&nbsp;and&nbsp;k&nbsp;where&nbsp;(0 &lt;= i &lt; j &lt;= k &lt; arr.length). Let&#8217;s define&nbsp;a&nbsp;and&nbsp;b&nbsp;as follows: a = arr[i] ^&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,177,97,63],"class_list":["post-6739","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-medium","tag-prefix","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6739","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=6739"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6739\/revisions"}],"predecessor-version":[{"id":6748,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6739\/revisions\/6748"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}