-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeakEle.java
More file actions
34 lines (31 loc) · 911 Bytes
/
PeakEle.java
File metadata and controls
34 lines (31 loc) · 911 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
import java.util.*;
public class PeakEle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
ArrayList<Integer> ls = new ArrayList<>();
for(int i = 0; i < arr.length; i++){
ls.add(arr[i]);
}
if(arr[0] > arr[1]){
ls.add(arr[0]);
}
if(arr[n-1] > arr[n-2]){
ls.add(arr[n-1]);
}
for(int i = 1; i < arr.length - 1; i++){
if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]){
ls.add(arr[i]);
}
}
int res[] = new int[ls.size()];
for(int i = 0; i < ls.size(); i++){
res[i] = ls.get(i);
}
System.out.println(Arrays.toString(res));
}
}