-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountTripletsOfEqualXOR.java
More file actions
38 lines (37 loc) · 1.34 KB
/
CountTripletsOfEqualXOR.java
File metadata and controls
38 lines (37 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class CountTripletsOfEqualXOR {
public int countTriplets(int[] arr) {
if (arr == null || arr.length == 0)
return 0;
int[][] computed = new int[arr.length][arr.length];
int count = 0;
for (int i = 0; i < arr.length; i++)
computed[i][i] = arr[i];
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
computed[i][j] = computed[i][j - 1] ^ arr[j];
}
}
for (int i = 0; i < computed.length; i++) {
for (int j = 0; j < computed[i].length; j++) {
System.out.print(computed[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
for (int k = j; k < arr.length; k++) {
if (computed[i][j - 1] == computed[j][k]) {
count++;
System.out.println(i + ", " + j + ", " + k);
}
}
}
}
return count;
}
public static void main(String[] args) {
CountTripletsOfEqualXOR ctoex = new CountTripletsOfEqualXOR();
int[] arr = new int [] {2, 3, 1, 6, 7};
System.out.println(ctoex.countTriplets(arr));
}
}