-
Notifications
You must be signed in to change notification settings - Fork 3
/
awkwardparty.java
35 lines (33 loc) · 1008 Bytes
/
awkwardparty.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
import java.util.*;
import java.math.*;
import java.io.*;
public class awkwardparty {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(in.readLine());
int[] locations = new int[1000001];
int[] mins = new int[1000001];
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i = 1; i <= n; i++) {
int x = Integer.parseInt(st.nextToken());
if(locations[x] != 0 && mins[x] != 0 && i - locations[x] < mins[x]) {
mins[x] = i - locations[x];
locations[x] = i;
}
else if(locations[x] != 0 && mins[x] == 0) {
mins[x] = i - locations[x];
locations[x] = i;
}
else {
locations[x] = i;
}
}
int min = Integer.MAX_VALUE;
for(int i : mins) {
if(i != 0 && i < min)
min = i;
}
System.out.println(min == Integer.MAX_VALUE ? n : min);
}
}