mountain classpptx
TRANSCRIPT
The MountainJohn Latimer
And
Kyle Whittall
Does it have a peak?
Are all values in the array increasing before the peak?
Are all values in the array decreasing after the peak?
Is the array a mountain?
If so….You have a mountain!
A high point in an array.
Example:
1, 2, 3, 4, 5, 4, 3, 2, 1
A peak is…
5
It is a mountain range!
If it does not have a single mountain…
1 2 31
3 2 2 3 3 21
23
21
Has multiple peaks…
The numbers are not only increasing and decreasing before and after one peak.
Example:
A mountain range…
0
2
4
6
8
C
Determine a peak index…
The peak index is the element whose value is greater than the value immediately before it and also greater than the value immediately following it.
The first step…
0
1
2
3
4
5
6
Series 1
Column2
Column1
The peak index here is 5
Determine if all values leading up to a peak are increasing
The second step is to…
Check that all values following the peak are decreasing
The third step is to…
There is a peak if all values before the highest number are increasing and all values following the highest number are decreasing
The code required for a mountain class…
public static int getPeakIndex(int[] arr)
{
// part a
}
To determine if all values after the peak are decreasing…
Next use a for loop to vary through the array
Use an if stmt to contrast i and i-1
You must then add to the if stmt to make it compare i and i+1
If the program finds a peak return that value
If the program does not find a peak have it return -1
Last is the return