Skip to content

Commit d06c310

Browse files
committedApr 14, 2025
Add solution #1442
1 parent 3700f1c commit d06c310

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,305 LeetCode solutions in JavaScript
1+
# 1,306 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1100,6 +1100,7 @@
11001100
1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](./solutions/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js)|Medium|
11011101
1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](./solutions/1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js)|Hard|
11021102
1441|[Build an Array With Stack Operations](./solutions/1441-build-an-array-with-stack-operations.js)|Medium|
1103+
1442|[Count Triplets That Can Form Two Arrays of Equal XOR](./solutions/1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js)|Medium|
11031104
1443|[Minimum Time to Collect All Apples in a Tree](./solutions/1443-minimum-time-to-collect-all-apples-in-a-tree.js)|Medium|
11041105
1446|[Consecutive Characters](./solutions/1446-consecutive-characters.js)|Easy|
11051106
1447|[Simplified Fractions](./solutions/1447-simplified-fractions.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 1442. Count Triplets That Can Form Two Arrays of Equal XOR
3+
* https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers arr.
7+
*
8+
* We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
9+
*
10+
* Let's define a and b as follows:
11+
* - a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
12+
* - b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
13+
*
14+
* Note that ^ denotes the bitwise-xor operation.
15+
*
16+
* Return the number of triplets (i, j and k) Where a == b.
17+
*/
18+
19+
/**
20+
* @param {number[]} arr
21+
* @return {number}
22+
*/
23+
var countTriplets = function(arr) {
24+
const prefixXor = [0];
25+
let result = 0;
26+
27+
for (const num of arr) {
28+
prefixXor.push(prefixXor[prefixXor.length - 1] ^ num);
29+
}
30+
31+
for (let i = 0; i < arr.length; i++) {
32+
for (let k = i + 1; k < arr.length; k++) {
33+
if ((prefixXor[k + 1] ^ prefixXor[i]) === 0) {
34+
result += k - i;
35+
}
36+
}
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
 (0)
Please sign in to comment.