#K84022. Hills and Valleys Count
Hills and Valleys Count
Hills and Valleys Count
You are given a list of elevations representing points along a marathon route. Your task is to determine the number of "hills" and "valleys" in the list. A hill is defined as an index i (1 ≤ i ≤ n-2) where the elevation is strictly greater than both its neighbors. Similarly, a valley is an index i where the elevation is strictly less than both its neighbors.
More formally, given a sequence \( a_0,a_1,\ldots,a_{n-1} \), an index \( i \) (with \( 1 \le i \le n-2 \)) is:
- a hill if \( a_{i-1} a_{i+1} \),
- a valley if \( a_{i-1} > a_i \) and \( a_i < a_{i+1} \).
When checking for hills and valleys, once a hill or valley is found at index \( i \), skip the next index to avoid double counting overlapping features.
The input is read from standard input and the output is written to standard output.
inputFormat
The input consists of two lines. The first line contains an integer \( n \) representing the number of points. The second line contains \( n \) space-separated integers that denote the elevation values.
Example:
8 1 3 2 5 4 6 7 2
outputFormat
Output a single integer: the total number of hills and valleys in the elevation list according to the given definition.
Example:
3## sample
8
1 3 2 5 4 6 7 2
3