Skip to content

Commit 56917f8

Browse files
authored
Create 1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js
1 parent 3cd991b commit 56917f8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
const countTriplets = function(arr) {
6+
arr.unshift(0)
7+
const n = arr.length
8+
let res = 0
9+
for(let i = 1; i < n; i++) {
10+
arr[i] ^= arr[i - 1]
11+
}
12+
const count = {}, total = {}
13+
for(let i = 0; i < n; i++) {
14+
if(count[arr[i]] == null) count[arr[i]] = 0
15+
if(total[arr[i]] == null) total[arr[i]] = 0
16+
res += count[arr[i]]++ * (i - 1) - total[arr[i]]
17+
total[arr[i]] += i
18+
}
19+
return res
20+
};

0 commit comments

Comments
 (0)