#C9409. Count Distinct Peaks
Count Distinct Peaks
Count Distinct Peaks
You are given a sequence of mountain heights. Your task is to count the number of peaks in the sequence. A peak is defined as an element that is strictly greater than its adjacent elements. For the first and the last mountain in the sequence, only one adjacent mountain is considered.
More formally, for a sequence \(h_0, h_1, \ldots, h_{n-1}\):
- For \(i = 0\), \(h_0\) is a peak if \(h_0 > h_1\).
- For \(i = n-1\), \(h_{n-1}\) is a peak if \(h_{n-1} > h_{n-2}\).
- For \(0 < i h_{i-1}\) and \(h_i > h_{i+1}\).
If the sequence is empty (i.e. \(n = 0\)), then the number of peaks is defined as 0. If there is only one mountain, it is considered as a peak.
inputFormat
The input is provided via standard input (stdin) and consists of two lines.
- The first line contains a single integer \(n\), the number of mountains.
- The second line contains \(n\) space-separated integers representing the heights of the mountains.
outputFormat
The output should be printed to standard output (stdout) and is a single integer representing the number of peaks in the sequence.
## sample7
3 5 3 4 6 2 1
2