Count Elements Greater Than Neighbor
EasyPractice library · +10 XParrayscounting
Given a list of N integers, count how many elements are strictly greater than both their immediate neighbors. The first and last elements only have one neighbor, so they are counted if they are strictly greater than their single neighbor. Elements in a list of size 1 are always counted.
Input
The first line contains an integer N (1 <= N <= 1000). The second line contains N integers separated by spaces.
Output
Print a single integer: the count of elements strictly greater than their neighbors.
Constraints
1 <= N <= 1000, -10^9 <= element <= 10^9
Time limit 1 s (native code; slower languages get more)Memory 256 MB17 hidden tests
Example 1
Input
5 1 5 2 6 3
Output
2
Elements 5 and 6 are greater than both neighbors.
Example 2
Input
3 10 20 15
Output
1
Elements 20 and 15 are greater than their neighbors (15 only has one neighbor).
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 5 2 6 3
Example 2 input
3 10 20 15