-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieMaxsubarray.cpp
More file actions
109 lines (78 loc) · 1.7 KB
/
TrieMaxsubarray.cpp
File metadata and controls
109 lines (78 loc) · 1.7 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<bits/stdc++.h>
using namespace std ;
class trie{
public : trie *left ;
trie *right ;
};
int compare_with_prefixes(trie *head,int curr_xor)
{
int val=0;
for(int i=31;i>=0;i--)
{
int temp=curr_xor>>i ;
if(temp&1)
{
if(head->left)
{
val+=pow(2,i) ;
head=head->left ;
}
else{
head=head->right ;
}
}
else{
if(head->right)
{
val+=pow(2,i);
head=head->right ;
}
else{
head=head->left ;
}
}
}
cout<<"the value in insertion is "<<val<<endl;
return val ;
}
void insert_it(trie *head,int curr_xor)
{
for(int i=31;i>=0;i--)
{
int temp=curr_xor>>i ;
if(temp&1)
{
if(!head->right)
{
head->right=new trie() ;
}
head=head->right ;
}
else{
if(!head->left)
{
head->left=new trie() ;
}
head=head->left ;
}
}
}
int main()
{
int n;
cin>>n;
int arr[n] ;
for(int i=0;i<n;i++)
cin>>arr[i] ;
trie *head=new trie() ;
int max_val=INT_MIN ;
int curr_xor=0;
for(int i=0;i<n;i++)
{
curr_xor=curr_xor^arr[i] ;
insert_it(head,curr_xor) ;
max_val=max(max_val,curr_xor) ;
max_val=max(max_val,compare_with_prefixes(head,curr_xor)) ;
}
cout<<max_val<<endl;
}