Li has planned a bike tour through the mountains of Switzerland. His tour consists of N checkpoints, numbered from 1 to N in the order he will visit them. The i-th checkpoint has a height of Hi.
A checkpoint is a peak if:
It is not the 1st checkpoint or the N-th checkpoint, and
The height of the checkpoint is strictly greater than the checkpoint immediately before it and the checkpoint immediately after it.
Please help Li find out the number of peaks.
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Hi.
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of peaks in Li's bike tour.
1 ≤ T ≤ 100. 1 ≤ Hi ≤ 100. 3 ≤ N ≤ 100.
Input: 4 3 10 20 14 4 7 7 7 7 5 10 90 20 90 10 3 10 3 10 Output: Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 0
This is a trivial problem where we need to check each element as per its constraints. So an element is peak if it's not the corner element and it's strictly greater than its neighbours. So the algorithm would be-
What can be done as part of optimizing further?
One think we can do is instead of checking each and every element to be a peak, we can skip few elements. If an element(arr[i]) is peak then we can skip the immediate right neighbour(arr[i+1]) as that can’t be peak as per constraints. This can be done in terms of optimization, but still will lead to O(n) complexity.
Since the algo is pretty intuitive and self-explanatory I am not going for dry run. However you can do a dry run for better understanding.