-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSubarray Max XOR.java
More file actions
56 lines (51 loc) · 1.43 KB
/
Copy pathSubarray Max XOR.java
File metadata and controls
56 lines (51 loc) · 1.43 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
// https://www.hackerrank.com/contests/smart-interviews/challenges/si-subarray-max-xor
import java.io.*;
import java.util.*;
public class Solution {
static class Trie{
Trie[] child = new Trie[2];
Trie(){
child[0]=child[1]=null;
}
}
static void insert(Trie root,int ele){
for(int i=31;i>=0;i--){
int bit = (ele>>i)&1;
if(root.child[bit]==null){
root.child[bit]=new Trie();
}
root=root.child[bit];
}
}
static int maxXor(Trie root,int ele){
int ans=0;
for(int i=31;i>=0;i--){
int bit = (ele>>i)&1;
if(root.child[bit^1]!=null){
ans=ans|(1<<i);
root=root.child[1-bit];
}else{
root=root.child[bit];
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
Trie root = new Trie();
int ans=0,pre=0;
insert(root,0);
for(int ele:a){
pre^=ele;
insert(root,pre);
ans=Math.max(ans,maxXor(root,pre));
}
System.out.println(ans);
}
}
}