#K37097. Counting Peaks in a Sequence
Counting Peaks in a Sequence
Counting Peaks in a Sequence
You are given a sequence of n integers. An element of the sequence, denoted as \(a_i\), is considered a peak if it is strictly greater than its neighbors. For the elements at the boundaries (i.e. the first and the last elements), there is only one neighbor, so an element is a peak if it is greater than that single neighbor.
Your task is to count the number of peaks present in each given sequence.
Definition:
For a sequence \(a_0, a_1, \ldots, a_{n-1}\):
- If \(n=1\), the sole element is a peak by definition.
- An element \(a_0\) is a peak if \(a_0 > a_1\).
- An element \(a_{n-1}\) is a peak if \(a_{n-1} > a_{n-2}\).
- For any other element \(a_i\) (\(0 < i < n-1\)), it is a peak if \(a_i > a_{i-1}\) and \(a_i > a_{i+1}\).
inputFormat
The input begins with an integer T
, the number of test cases. Each test case is described as follows:
- The first line contains an integer
n
, the length of the sequence. - The second line contains
n
space-separated integers representing the sequence.
Input is read from stdin
.
outputFormat
For each test case, output a single integer on a new line which is the number of peaks in the sequence.
Output is written to stdout
.
2
6
1 3 2 4 1 0
5
10 20 15 2 23
2
2
</p>