-
Notifications
You must be signed in to change notification settings - Fork 0
/
NicholasAndPermutation.java
46 lines (38 loc) · 1.12 KB
/
NicholasAndPermutation.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
/* https://codeforces.com/contest/676/problem/A
#constructive-algorithms #implementation
get the index of max value, min value.
result is max(length - 1 - the greater index, smaller index)
*/
import java.util.Scanner;
public class NicholasAndPermutation {
static int calMaxDistance() {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int maxIdx = 0;
int minIdx = 0;
int maxValue = scanner.nextInt();
int minValue = maxValue;
for (int i = 1; i < length; i++) {
int temp = scanner.nextInt();
if (temp < minValue) {
minValue = temp;
minIdx = i;
}
if (temp > maxValue) {
maxValue = temp;
maxIdx = i;
}
}
int result = 0;
// result is max(length - 1 - the greater index, smaller index)
if (maxIdx > minIdx) {
result = maxIdx > (length - minIdx - 1) ? maxIdx : (length - minIdx - 1);
} else {
result = minIdx > (length - maxIdx - 1) ? minIdx : (length - maxIdx - 1);
}
return result;
}
public static void main(String args[]) {
System.out.println(calMaxDistance());
}
}