-
Notifications
You must be signed in to change notification settings - Fork 0
/
XorSubsequences.java
74 lines (70 loc) · 2.47 KB
/
XorSubsequences.java
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
import java.util.*;
public class XorSubsequences {
public static int maxSubsequenceLength(int n, int k, List<Integer> arr) {
int[] counts = new int[1048576];
Set<String> options = new HashSet<>();
for(int a: arr){
StringBuilder option = new StringBuilder();
if(counts[a ^ k] > 0){
option.append(a).append(" ").append(a ^ k);
options.add(option.toString());
}
counts[a]++;
}
int currentMax = 1;
for(String option: options){
StringTokenizer tokenizer = new StringTokenizer(option);
int a = Integer.parseInt(tokenizer.nextToken());
int b = Integer.parseInt(tokenizer.nextToken());
int[] two = new int[]{a, b};
int i_a = 0;
int j_a = 0;
int currentLong = 0;
while(j_a < arr.size()){
if(arr.get(j_a) == two[i_a]){
i_a++;
currentLong++;
}
if(i_a > 1){
i_a = 0;
}
j_a++;
}
int ans = currentLong;
currentLong = 0;
j_a = 0;
i_a = 0;
while(j_a < arr.size()){
if(arr.get(j_a) == two[(i_a + 1) % 2]){
i_a++;
currentLong++;
}
if(i_a > 1){
i_a = 0;
}
j_a++;
}
ans = Math.max(currentLong, ans);
currentMax = Math.max(currentMax, ans);
}
return currentMax;
}
public static void main(String[] args) {
List<Integer> items = Arrays.asList(2,1,3,5,2);
List<Integer> items2 = Arrays.asList(121,144,121,144,121,144,121,144,121);
List<Integer> items3 = Arrays.asList(1,1,1);
List<Integer> items4 = Collections.singletonList(100000);
List<Integer> items5 = Arrays.asList(4,3,9, 14,4,3, 9);
int k = 2;
int k2 = 233;
int k3 = 0;
int k4 = 1;
int k5 = 10;
int ans = maxSubsequenceLength(items.size(), k, items);
int ans2 = maxSubsequenceLength(items2.size(), k2, items2);
int ans3 = maxSubsequenceLength(items3.size(), k3, items3);
int ans4 = maxSubsequenceLength(items4.size(), k4, items4);
int ans5 = maxSubsequenceLength(items5.size(), k5, items5);
System.out.println(ans5);
}
}