Algorithm Analysis
Let the array contain the input sequence of numbers. The numbering of the array elements starts from 0. The array contains numbers. The element has two neighbors if only .
To count the number of elements that are greater than their two neighbors, it is necessary to iterate through all such that , and check the fulfillment of two conditions: and .
Algorithm Implementation
Declare the working array.
int m[101];
Read the number of input numbers .
scanf("%d", &n);
Read the input array.
for (i = 0; i < n; i++) scanf("%d", &m[i]);
In the variable res
, count the number of elements that are greater than their two neighbors.
res = 0;
Iterate through all such that and check the fulfillment of two conditions: and .
for (i = 1; i < n - 1; i++) if (m[i] > m[i - 1] && m[i] > m[i + 1]) res++;
Output the answer.
printf("%d\n", res);