Skip to content

Commit 828d8e4

Browse files
Create 1442-count-triplets-that-can-form-two-arrays-of-equal-xor.java
1 parent d068e08 commit 828d8e4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*-------------------------------
2+
Time Complexity : O(n^3)
3+
Space Complexity : O(1)
4+
-------------------------------*/
5+
class Solution {
6+
public int countTriplets(int[] arr) {
7+
int res = 0;
8+
9+
for(int i = 0; i < arr.length - 1; i++){
10+
int a = 0;
11+
for(int j = i + 1; j < arr.length; j++){
12+
a ^= arr[j - 1];
13+
int b = 0;
14+
for(int k = j; k < arr.length; k++){
15+
b ^= arr[k];
16+
if(a == b)
17+
res += 1;
18+
}
19+
}
20+
}
21+
return res;
22+
}
23+
}
24+
25+
/*-------------------------------
26+
Time Complexity : O(n^2)
27+
Space Complexity : O(1)
28+
-------------------------------*/
29+
class Solution {
30+
public int countTriplets(int[] arr) {
31+
int res = 0;
32+
33+
for(int i = 0; i < arr.length - 1; i++){
34+
int cur_xor = arr[i];
35+
for(int k = i + 1; k < arr.length; k++){
36+
cur_xor ^= arr[k];
37+
if(cur_xor == 0)
38+
res += k - i;
39+
}
40+
}
41+
return res;
42+
}
43+
}

0 commit comments

Comments
 (0)