-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppendingMex.java
46 lines (40 loc) · 1.19 KB
/
AppendingMex.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
import java.util.* ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
public class AppendingMex
{
public static void main(String args[]) throws Exception
{
BufferedReader bro = new BufferedReader(new InputStreamReader(System.in)) ;
int N = Integer.parseInt(bro.readLine()) ;
String[] S = bro.readLine().split(" ") ;
int[] A = new int[N] ;
HashSet<Integer> H = new HashSet<Integer>() ;
for(int i=0;i<N;i++) A[i] = Integer.parseInt(S[i]) ;
boolean print = false ;
if(A[0]!=0)
System.out.println("1") ;
else
{
H.add(0) ;
for(int i=1;i<N;i++)
{
if(!H.contains(A[i]))
{
if(H.contains(A[i]-1))
{
H.add(A[i]) ;
}
else
{
System.out.println(i+1);
print = true ;
break ;
}
}
}
if(!print)
System.out.println("-1");
}
}
}