|
6 | 6 |
|
7 | 7 | <p>Given an array of integers <code>arr</code>.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <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>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <p>Let's define <code>a</code> and <code>b</code> as follows:</p>
|
16 | 12 |
|
17 |
| - |
18 |
| - |
19 | 13 | <ul>
|
20 | 14 | <li><code>a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]</code></li>
|
21 | 15 | <li><code>b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]</code></li>
|
22 | 16 | </ul>
|
23 | 17 |
|
24 |
| - |
25 |
| - |
26 | 18 | <p>Note that <strong>^</strong> denotes the <strong>bitwise-xor</strong> operation.</p>
|
27 | 19 |
|
28 |
| - |
29 |
| - |
30 | 20 | <p>Return <em>the number of triplets</em> (<code>i</code>, <code>j</code> and <code>k</code>) Where <code>a == b</code>.</p>
|
31 | 21 |
|
32 |
| - |
33 |
| - |
34 | 22 | <p> </p>
|
35 | 23 |
|
36 | 24 | <p><strong>Example 1:</strong></p>
|
37 | 25 |
|
38 |
| - |
39 |
| - |
40 | 26 | <pre>
|
41 | 27 |
|
42 | 28 | <strong>Input:</strong> arr = [2,3,1,6,7]
|
|
47 | 33 |
|
48 | 34 | </pre>
|
49 | 35 |
|
50 |
| - |
51 |
| - |
52 | 36 | <p><strong>Example 2:</strong></p>
|
53 | 37 |
|
54 |
| - |
55 |
| - |
56 | 38 | <pre>
|
57 | 39 |
|
58 | 40 | <strong>Input:</strong> arr = [1,1,1,1,1]
|
|
61 | 43 |
|
62 | 44 | </pre>
|
63 | 45 |
|
64 |
| - |
65 |
| - |
66 | 46 | <p><strong>Example 3:</strong></p>
|
67 | 47 |
|
68 |
| - |
69 |
| - |
70 | 48 | <pre>
|
71 | 49 |
|
72 | 50 | <strong>Input:</strong> arr = [2,3]
|
|
75 | 53 |
|
76 | 54 | </pre>
|
77 | 55 |
|
78 |
| - |
79 |
| - |
80 | 56 | <p><strong>Example 4:</strong></p>
|
81 | 57 |
|
82 |
| - |
83 |
| - |
84 | 58 | <pre>
|
85 | 59 |
|
86 | 60 | <strong>Input:</strong> arr = [1,3,5,7,9]
|
|
89 | 63 |
|
90 | 64 | </pre>
|
91 | 65 |
|
92 |
| - |
93 |
| - |
94 | 66 | <p><strong>Example 5:</strong></p>
|
95 | 67 |
|
96 |
| - |
97 |
| - |
98 | 68 | <pre>
|
99 | 69 |
|
100 | 70 | <strong>Input:</strong> arr = [7,11,12,9,5,2,7,17,22]
|
|
103 | 73 |
|
104 | 74 | </pre>
|
105 | 75 |
|
106 |
| - |
107 |
| - |
108 | 76 | <p> </p>
|
109 | 77 |
|
110 | 78 | <p><strong>Constraints:</strong></p>
|
111 | 79 |
|
112 |
| - |
113 |
| - |
114 | 80 | <ul>
|
115 | 81 | <li><code>1 <= arr.length <= 300</code></li>
|
116 | 82 | <li><code>1 <= arr[i] <= 10^8</code></li>
|
|
123 | 89 | ### **Python3**
|
124 | 90 |
|
125 | 91 | ```python
|
126 |
| - |
| 92 | +class Solution: |
| 93 | + def countTriplets(self, arr: List[int]) -> int: |
| 94 | + n = len(arr) |
| 95 | + pre = [0] * (n + 1) |
| 96 | + for i in range(n): |
| 97 | + pre[i + 1] = pre[i] ^ arr[i] |
| 98 | + ans = 0 |
| 99 | + for i in range(n - 1): |
| 100 | + for j in range(i + 1, n): |
| 101 | + for k in range(j, n): |
| 102 | + a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j] |
| 103 | + if a == b: |
| 104 | + ans += 1 |
| 105 | + return ans |
127 | 106 | ```
|
128 | 107 |
|
129 | 108 | ### **Java**
|
130 | 109 |
|
131 | 110 | ```java
|
| 111 | +class Solution { |
| 112 | + public int countTriplets(int[] arr) { |
| 113 | + int n = arr.length; |
| 114 | + int[] pre = new int[n + 1]; |
| 115 | + for (int i = 0; i < n; ++i) { |
| 116 | + pre[i + 1] = pre[i] ^ arr[i]; |
| 117 | + } |
| 118 | + int ans = 0; |
| 119 | + for (int i = 0; i < n - 1; ++i) { |
| 120 | + for (int j = i + 1; j < n; ++j) { |
| 121 | + for (int k = j; k < n; ++k) { |
| 122 | + int a = pre[j] ^ pre[i]; |
| 123 | + int b = pre[k + 1] ^ pre[j]; |
| 124 | + if (a == b) { |
| 125 | + ++ans; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return ans; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### **C++** |
| 136 | + |
| 137 | +```cpp |
| 138 | +class Solution { |
| 139 | +public: |
| 140 | + int countTriplets(vector<int>& arr) { |
| 141 | + int n = arr.size(); |
| 142 | + vector<int> pre(n + 1); |
| 143 | + for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i]; |
| 144 | + int ans = 0; |
| 145 | + for (int i = 0; i < n - 1; ++i) |
| 146 | + { |
| 147 | + for (int j = i + 1; j < n; ++j) |
| 148 | + { |
| 149 | + for (int k = j; k < n; ++k) |
| 150 | + { |
| 151 | + int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j]; |
| 152 | + if (a == b) ++ans; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return ans; |
| 157 | + } |
| 158 | +}; |
| 159 | +``` |
132 | 160 |
|
| 161 | +### **Go** |
| 162 | +
|
| 163 | +```go |
| 164 | +func countTriplets(arr []int) int { |
| 165 | + n := len(arr) |
| 166 | + pre := make([]int, n+1) |
| 167 | + for i := 0; i < n; i++ { |
| 168 | + pre[i+1] = pre[i] ^ arr[i] |
| 169 | + } |
| 170 | + ans := 0 |
| 171 | + for i := 0; i < n-1; i++ { |
| 172 | + for j := i + 1; j < n; j++ { |
| 173 | + for k := j; k < n; k++ { |
| 174 | + a, b := pre[j]^pre[i], pre[k+1]^pre[j] |
| 175 | + if a == b { |
| 176 | + ans++ |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return ans |
| 182 | +} |
133 | 183 | ```
|
134 | 184 |
|
135 | 185 | ### **...**
|
|
0 commit comments