Skip to content

Commit 8244f72

Browse files
authored
Update 1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js
1 parent 9817c60 commit 8244f72

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1442-count-triplets-that-can-form-two-arrays-of-equal-xor.js

+28
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,31 @@ const countTriplets = function(arr) {
6767
}
6868
return res
6969
};
70+
/*
71+
72+
you have an array : a[0], a[1].... a[n - 1]
73+
74+
75+
First things first:
76+
We need to understand small fact, if xor(a[0....i]) has appeared before at index j then it means xor(a[j+1.....i]) = 0
77+
Another fact, if xor(a[i....j]) = 0 so this subarray will add (j - i - 1) to the answer.
78+
79+
80+
Now say currently we are at index i and let xor([0...i]) = x.
81+
82+
83+
Now say x has occurred 3 times previously at indices (i1, i2, i3)
84+
85+
86+
our answer for i will be = (i - i1 - 1) + (i - i2 - 1) + (i - i3 - 1)
87+
88+
89+
if you simplify this further you get f * i - (i1 + i2 + i3) - f = (i - 1) * f - (i1 + i2 + i3)
90+
91+
92+
f = no. of times x has occurred previously.
93+
94+
95+
(i1 + i2 + i3) = sum of all the indices where x has occurred previously.
96+
97+
*/

0 commit comments

Comments
 (0)