|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../build-an-array-with-stack-operations "Build an Array With Stack Operations") |
| 9 | + |
| 10 | +[Next >](../minimum-time-to-collect-all-apples-in-a-tree "Minimum Time to Collect All Apples in a Tree") |
| 11 | + |
| 12 | +## [1442. Count Triplets That Can Form Two Arrays of Equal XOR (Medium)](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor "形成两个异或相等数组的三元组数目") |
| 13 | + |
| 14 | +<p>Given an array of integers <code>arr</code>.</p> |
| 15 | + |
| 16 | +<p>We want to select three indices <code>i</code>, <code>j</code> and <code>k</code> where <code>(0 <= i < j <= k < arr.length)</code>.</p> |
| 17 | + |
| 18 | +<p>Let's define <code>a</code> and <code>b</code> as follows:</p> |
| 19 | + |
| 20 | +<ul> |
| 21 | + <li><code>a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]</code></li> |
| 22 | + <li><code>b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]</code></li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p>Note that <strong>^</strong> denotes the <strong>bitwise-xor</strong> operation.</p> |
| 26 | + |
| 27 | +<p>Return <em>the number of triplets</em> (<code>i</code>, <code>j</code> and <code>k</code>) Where <code>a == b</code>.</p> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Example 1:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> arr = [2,3,1,6,7] |
| 34 | +<strong>Output:</strong> 4 |
| 35 | +<strong>Explanation:</strong> The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4) |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>Example 2:</strong></p> |
| 39 | + |
| 40 | +<pre> |
| 41 | +<strong>Input:</strong> arr = [1,1,1,1,1] |
| 42 | +<strong>Output:</strong> 10 |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p><strong>Example 3:</strong></p> |
| 46 | + |
| 47 | +<pre> |
| 48 | +<strong>Input:</strong> arr = [2,3] |
| 49 | +<strong>Output:</strong> 0 |
| 50 | +</pre> |
| 51 | + |
| 52 | +<p><strong>Example 4:</strong></p> |
| 53 | + |
| 54 | +<pre> |
| 55 | +<strong>Input:</strong> arr = [1,3,5,7,9] |
| 56 | +<strong>Output:</strong> 3 |
| 57 | +</pre> |
| 58 | + |
| 59 | +<p><strong>Example 5:</strong></p> |
| 60 | + |
| 61 | +<pre> |
| 62 | +<strong>Input:</strong> arr = [7,11,12,9,5,2,7,17,22] |
| 63 | +<strong>Output:</strong> 8 |
| 64 | +</pre> |
| 65 | + |
| 66 | +<p> </p> |
| 67 | +<p><strong>Constraints:</strong></p> |
| 68 | + |
| 69 | +<ul> |
| 70 | + <li><code>1 <= arr.length <= 300</code></li> |
| 71 | + <li><code>1 <= arr[i] <= 10^8</code></li> |
| 72 | +</ul> |
| 73 | + |
| 74 | +### Related Topics |
| 75 | + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] |
| 76 | + [[Array](../../tag/array/README.md)] |
| 77 | + [[Math](../../tag/math/README.md)] |
| 78 | + |
| 79 | +### Hints |
| 80 | +<details> |
| 81 | +<summary>Hint 1</summary> |
| 82 | +We are searching for sub-array of length ≥ 2 and we need to split it to 2 non-empty arrays so that the xor of the first array is equal to the xor of the second array. This is equivalent to searching for sub-array with xor = 0. |
| 83 | +</details> |
| 84 | + |
| 85 | +<details> |
| 86 | +<summary>Hint 2</summary> |
| 87 | +Keep the prefix xor of arr in another array, check the xor of all sub-arrays in O(n^2), if the xor of sub-array of length x is 0 add x-1 to the answer. |
| 88 | +</details> |
0 commit comments