Skip to content

Commit 653e5dc

Browse files
committed
new soln
1 parent 6694fc2 commit 653e5dc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: 1442.CountTriplets.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 1442. Count Triplets That Can Form Two Arrays of Equal XOR
2+
// Given an array of integers arr.
3+
// We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).
4+
// Let's define a and b as follows:
5+
// a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
6+
// b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
7+
// Note that ^ denotes the bitwise-xor operation.
8+
// Return the number of triplets (i, j and k) Where a == b.
9+
10+
// Example 1:
11+
// Input: arr = [2,3,1,6,7]
12+
// Output: 4
13+
// Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
14+
// Example 2:
15+
// Input: arr = [1,1,1,1,1]
16+
// Output: 10
17+
18+
// Constraints:
19+
// 1 <= arr.length <= 300
20+
// 1 <= arr[i] <= 108
21+
22+
public class Solution {
23+
public int CountTriplets(int[] arr) {
24+
int count = 0;
25+
for(int i = 0; i < arr.Length; i++){
26+
int val = arr[i];
27+
for(int k = i+1; k < arr.Length; k++){
28+
val ^= arr[k];
29+
if(val == 0)
30+
count += (k-i);
31+
}
32+
}
33+
return count;
34+
}
35+
}

0 commit comments

Comments
 (0)