-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMountainScenery.java
More file actions
40 lines (40 loc) · 1.38 KB
/
MountainScenery.java
File metadata and controls
40 lines (40 loc) · 1.38 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
import java.util.Scanner;
public class MountainScenery {
public static void main(String[] arags) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), peaks = sc.nextInt(), index = 0;
int[] a = new int[2 * n + 1];
while(sc.hasNextInt()) {
a[index] = sc.nextInt();
index++;
}
sc.close();
int count = 0;
int[] indexes = new int[peaks];
if(a[0] > a[1]) {
// peak bottom peak
if(a[0] - a[1] >= 2) indexes[count++] = 0;
for(int i = 2; i < a.length - 1; i += 2) {
if(count == peaks) break;
if(a[i] - a[i - 1] >= 2 && a[i] - a[i + 1] >= 2) indexes[count++] = i;
}
} else {
// bottom peak bottom
for(int i = 1; i < a.length - 1; i += 2) {
if(count == peaks) break;
if(a[i] - a[i - 1] >= 2 && a[i] - a[i + 1] >= 2) indexes[count++] = i;
}
}
printInitialEles(indexes, a);
}
private static void printInitialEles(int[] indexes, int[] a) {
for(int i = 0, j = 0; i < a.length; i++) {
if(indexes[j] == i) {
if(j < indexes.length - 1) j++;
System.out.print(a[i] - 1 + " ");
} else {
System.out.print(a[i] + " ");
}
}
}
}