forked from ShashwatAwasthi04/Coding-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMEX.txt
More file actions
46 lines (44 loc) · 949 Bytes
/
Copy pathMEX.txt
File metadata and controls
46 lines (44 loc) · 949 Bytes
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.*;
class Main{
public static void main(String args[])
{ Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int ar[]=new int[k];
int count=0;
int arrval=0;
int ans=-1;
int start=0,end=0;
while(end<n)
{
if(a[end]<k )
{
if(ar[a[end]]==0)
count++;
ar[a[end]]++;
arrval++;
}
while(count==k)
{
if(ans==-1 || ans>(end-start+1))
{
if(arrval!=(end-start+1))
ans=end-start+1;
}
if(a[start]<k)
{
ar[a[start]]--;
if(ar[a[start]]==0)
count--;
arrval--;
}
start++;
}
end++;
}
System.out.println(ans);
}
}