File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments