-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeakFinder.java
44 lines (37 loc) · 1.11 KB
/
PeakFinder.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
package Arrays;
import java.util.Scanner;
public class PeakFinder {
public static void findPeak(int array[], int len) {
int peak[] = new int[len];
for (int i = 1, j = 0; i < (array.length - 1); i++) { // 1 to 1 less than the length of the array
if (array[i] > array[i + 1] && array[i] > array[i - 1] ) {
peak[j] = array[i];
j++;
}
if (array[0] > array[1] ) {
int peakStart = array[0];
System.out.println(peakStart);
}
}
for (int k = 0; k < peak.length; k++) {
if (peak[k]!=0)
System.out.println(peak[k]);
}
if (array[array.length-1] > array[array.length -2]) {
int peakEnd = array[array.length-1];
System.out.println(peakEnd);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of elements in an array");
int len = sc.nextInt();
int array[] = new int[len];
System.out.println("Enter elements");
for (int i = 0; i < len; i++) {
array[i] = sc.nextInt();
}
findPeak(array, len);
sc.close();
}
}