-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMex.java
More file actions
54 lines (42 loc) · 1.26 KB
/
Copy pathMaxMex.java
File metadata and controls
54 lines (42 loc) · 1.26 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
import java.util.*;
public class MaxMex {
// mex algorithm
public static long mex(Set<Long> a) {
long mex=0;
while(a.contains(mex))
mex++;
return mex;
}
// solve
public static void solve(Set<Long> a, int n ,int k, long max) {
long mex = mex(a) ;
if(mex> max || k==0 ) System.out.println(n+k);
else {
long s = (long )Math.ceil(((double)(max+mex))/2);
if(a.contains(s))
System.out.println(n) ;
else
System.out.println(n+1);
}
}
// main methode
public static void main(String args[]) {
Scanner in = new Scanner(System.in) ;
Set<Long> a= new HashSet<Long>() ;
int t = in.nextInt() ;
while(t>0) {
int n = in.nextInt() ;
int k = in.nextInt();
long max=0l;
for (int i=0 ; i<n ; i++) {
long x =in.nextLong();
if(x>max)
max=x;
a.add(x);
}
solve(a,n,k,max);
a.clear();
t--;
}
}
}