Problem:
 Score : 200 points    Problem Statement We have a permutation p = { p_1,\ p_2,\ ...,\ p_n } of { 1,\ 2,\ ...,\ n }.  Print the number of elements p_i ( 1 < i < n ) that satisfy the following condition:   p_i is the second smallest number among the three numbers p_{i - 1} , p_i , and p_{i + 1} .       Constraints  All values in input are integers.  3 \leq n \leq 20  p is a permutation of { 1,\ 2,\ ...,\ n }.         Input Input is given from Standard Input in the following format:  n  p_1  p_2  ...  p_n       Output Print the number of elements p_i ( 1 < i < n ) that satisfy the condition.        Sample Input 1 5
1 3 5 4 2      Sample Output 1 2  p_2 = 3 is the second smallest number among p_1 = 1 , p_2 = 3 , and p_3 = 5 . Also, p_4 = 4 is the second smallest number among p_3 = 5 , p_4 = 4 , and p_5 = 2 . These two elements satisfy the condition.       Sample Input 2 9
9 6 3 2 5 8 7 4 1      Sample Output 2 5

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    p = LI()
    a = [0]*(n+1)
    a[p[0]] += 1
    a[p[1]] += 1
    a[p[2]] += 1
    ans = 0
    if sum(a[:p[1]]) == 1:
        ans += 1
    for i in range(2,n-1):
        a[p[i-2]] -= 1
        a[p[i+1]] += 1
        if sum(a[:p[i]]) == 1:
            ans += 1
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(n-2):
    if P[i] < P[i+1] < P[i+2] or P[i] > P[i+1] > P[i+2]:
        ans += 1
print(ans)

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
ans = 0
for a,b,c in zip(P,P[1:],P[2:]):
    if a>b>c or a<b<c:
        ans += 1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
  if sorted([p[i-1],p[i],p[i+1]])[1]==p[i]:ans+=1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(1,n-1):
  if (p[i]>p[i-1] and p[i]<p[i+1]) or (p[i]<p[i-1] and p[i]>p[i+1]):
    count += 1
print(count)

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

n = inp()
p = inpl()
cnt = 0
for i in range(1, n+1):
    if i <= n-2:
        if p[i-1] < p[i] < p[i+1] or p[i-1] > p[i] > p[i+1]:
            cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(n-2):
    if p[i] < p[i+1] < p[i+2] or p[i+2] < p[i+1] < p[i]:
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0

for i in range(1,n-1):
  if p[i-1] < p[i] < p[i+1] or p[i+1] < p[i] < p[i-1]:
    count += 1
    
print(count)

Solution (Python):
n  = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(1,n-1):
    p_1 = p[i-1]
    p_2 = p[i]
    p_3 = p[i+1]
    if p_1 < p_2 < p_3 or p_1 > p_2 > p_3:
        count += 1
print(count)

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
count=0
for i in range(1,len(P)):
    
    if P[i]!=max(P[i-1:i+2]) and P[i]!=min(P[i-1:i+2]):
        count+=1
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n-2):
  if p[i] < p[i+1] < p[i+2] or p[i] > p[i+1] > p[i+2]:
    count += 1
print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(n-2):
    if p[i+1] != max(p[i:i+3]) and p[i+1] != min(p[i:i+3]):
        ans += 1
print(ans)

Solution (Python):
n = int(input())
s = list(map(int,input().split()))
ans = 0
for i in range(n-2):
    a,b,c = s[i:i+3]
    if a<b<c or c<b<a:  ans += 1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
    if p[i] != min(p[i - 1:i + 2]) and p[i] != max(p[i - 1:i + 2]):
        ans += 1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input()) 

nums = [int(i) for i in input().split()] 

ans = 0
for i in range(1, n - 1):
    a = nums[i - 1]
    b = nums[i]
    c = nums[i + 1]
    p = [a, b, c]
    p.sort()
    if p[1] == b:
        ans += 1
print(ans)

Solution (Python):
n=int(input())
ans=0
l=list(map(int,input().split()))
for i in range(1,n-1):
  p=max(l[i-1],l[i+1])
  q=min(l[i-1],l[i+1])
  if q<=l[i]<=p:ans+=1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
seconds = []
for a in range(0,n-2):
    num = [p[a],p[a+1],p[a+2]]
    if (num[0]<num[1]<num[2])or(num[2]<num[1]<num[0]):
        seconds.append(p[a+1])
    else : pass

print(len(set(seconds)))

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
count=0
for i in range(n-2):
  if((p[i]<p[i+1] and p[i+1]<p[i+2])or(p[i]>p[i+1] and p[i+1]>p[i+2])):
    count+=1
print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(1,n-1):
     if p[i-1] < p[i] < p[i+1] or p[i-1] > p[i] > p[i+1]:
          ans += 1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(1,n-1):
    if (p[i-1] > p[i] and p[i+1] < p[i]) or (p[i-1] < p[i] and p[i+1] > p[i]):
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
cnt=0
for i in range(n-2):
    p = P[i:i+3]
    p_s = sorted(p)
    if p[1]==p_s[1]:
        cnt+=1
print(cnt)

Solution (Python):
n = int(input())
pli = list(map(int,input().split()))
count = 0
for i in range(n-2):
    if pli[i] < pli[i+1] < pli[i+2] or pli[i] > pli[i+1] > pli[i+2]:
        count += 1
print(count)

Solution (Python):
n=int(input())
p=[int(i) for i in input().split()]
count=0
for t in range(1,n-1):
    if p[t-1]<p[t]<p[t+1] or p[t-1]>p[t]>p[t+1]:
        count+=1
    else:
        continue
print(count)

Solution (Python):
import io

nim = int(input())
 
mike = list(map(int,input().split()))
 
counter = 0

for i in range(nim):
    t = mike[i:i+3]
    if len(t) == 3:
        if max(t) > t[1] and min(t) < t[1]:
            counter += 1
            
            
print(counter)

Solution (Python):
n=int(input())
p=list(map(int, input().split()))
count = 0
for i in range(n-2):
  if p[i+1] != min(p[i:i+3]) and p[i+1] != max(p[i:i+3]):
    count = count + 1
print(count)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(n-2):
    a,b,c=p[i:i+3]
    if a<b<c or c<b<a:  cnt+=1
print(cnt)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(n-2):
    mn=min(p[i:i+3])
    mx=max(p[i:i+3])
    if mn!=p[i+1] and mx!=p[i+1]:
        cnt+=1
print(cnt)

Solution (Python):
n=int(input())
p=list(map(int, input().split()))
t=0
for i in range(n):
    if i!=0 and i!=n-1:
      l=[p[i-1], p[i], p[i+1]]
      l.sort()
      if l[1]==p[i]:
          t+=1
print(t)

Solution (Python):

def is_center(A,i):
    if (A[i-1]<A[i] and A[i]<A[i+1]) or (A[i-1]>A[i] and A[i]>A[i+1]):
        return True
    return False

N = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(1,N-1):
    if is_center(A,i):
        cnt+= 1
print(cnt)

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]

ans = 0
for i in range(1, n-1):
    if p[i-1] < p[i] < p[i+1]:
        ans += 1
    elif p[i-1] > p[i] > p[i+1]:
        ans += 1
    else:
        pass
print(ans)

Solution (Python):
n = int(input())
num = list(map(int, input().split()))

count = 0
for i in range(1,n-1):
  num1 = num[i-1:i+2]
  num2 = sorted(num1)
  if num2[1]==num1[1]: count += 1
    
print(count)

Solution (Python):
n = int(input())
p = input().split()
p = [int(x) for x in p]
c = 0
for i in range(1,n-1):
  if (p[i-1] <= p[i] and p[i] <= p[i+1]) or (p[i+1] <= p[i] and p[i] <= p[i-1]):
    c +=1
print(c)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

count = 0

for i in range (n-2):
    if p[i] < p[i+1] < p[i+2] or p[i] > p[i+1] > p[i+2]:
        count += 1

print(count)

Solution (Python):
n=int(input())
p=[int(_) for _ in input().split()]
count=0
for i in range(n-2):
    if p[i+1]!=min(p[i:i+3]) and p[i+1]!=max(p[i:i+3]):
        count+=1
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n-1):
    if p[i-1] < p[i] <= p[i+1] or p[i+1] < p[i] <= p[i-1]:
        ans += 1
print(ans)

Solution (Python):
n = int(input())

p = list(map(int, input().split()))

ans = 0

for i in range(1,n-1):
  if p[i-1] > p[i] > p[i+1] or p[i-1] < p[i] < p[i+1]:
    ans += 1

print(ans)

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
ANS=0
ans=p[0]-p[1]
for i in range(2,N):
    if (p[i-1]-p[i])*ans>=0:
        ANS+=1
    ans=(p[i-1]-p[i])//abs(p[i-1]-p[i])
print(ANS)

Solution (Python):
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
ps=LI()
compare=[]
count=0
for i in range(1,n-1):
    compare=[]
    compare.append(ps[i-1])
    compare.append(ps[i])
    compare.append(ps[i+1])
    s=sorted(compare)
    if ps[i]==s[1]:
        count+=1
print(count)

Solution (Python):
i=input()
m=list(map(int,input().split()))
c=0
for i in range(1,len(m)-1):
    if m[i - 1] > m[i] > m[i + 1]:
        c+=1
    elif m[i - 1] < m[i] < m[i + 1]:
        c+=1
print(c)

Solution (Python):
x = input()
w = [int(i) for i in input().split()]
d = 0
for i in range(len(w) -2 ):
    if (w[i] > w[i+1] > w[i+2]) or (w[i] < w[i+1] < w[i+2]):
        d += 1

print(d)

Solution (Python):
n = int(input())
numbers = list(map(int,input().split()))
count = 0
for i in range(1,n-1,1):
    if (numbers[i]>=numbers[i-1] and numbers[i]<=numbers[i+1]) or (numbers[i]>=numbers[i+1] and numbers[i]<=numbers[i-1]):
        count += 1

print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))

ans = 0

for i in range(1, n - 1):
    if p[i-1]<p[i]<p[i+1] or p[i-1]>p[i]>p[i+1]:
        ans += 1

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(1, n - 1):
    if (p[i - 1] <= p[i] and p[i] <= p[i + 1]) or (p[i + 1] <= p[i] and p[i] < p[i - 1]):
        cnt += 1

print(cnt)

Solution (Python):
N = int(input())
List = list(map(int,input().split()))
Count = 0
for i in range(N-2):
  if List[i] < List[i+1] < List[i+2] or List[i] > List[i+1] > List[i+2]:
    Count += 1

print(Count)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
x=0
q=""
for i in range(n-2):
    q=p[i:i+3]
    if q[1]!=max(q) and q[1]!=min(q):
        x+=1
print(x)

Solution (Python):
n = int(input())
pp = list(map(int, input().split()))


count = 0
for i in range(1, n - 1):
    partial = sorted(pp[i - 1:i + 2])
    if partial[1] == pp[i]:
        count += 1

print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

ans = 0
for i in range(1,n-1):
  if p[i] > p[i-1] and p[i] < p[i+1]:
    ans += 1
  elif p[i] < p[i-1] and p[i] > p[i+1]:
    ans += 1

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n-2):
    a, b, c = p[i], p[i+1], p[i+2]
    if a <= b <= c or c <= b <= a:
        count += 1
print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
print(len([True for i in range(1,n-1) if max(a[i-1:i+2])!=a[i] and min(a[i-1:i+2])!=a[i]]))

Solution (Python):
n = int(input())
p = list(map(int ,input().split()))
ans =0

for i in range(1 ,n-1):
    
    if p[i-1] <p[i] <p[i+1] or p[i-1] >p[i] >p[i+1]:
        ans +=1
       
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))

count = 0
for i in range(1, n-1):
    if p[i-1] < p[i] < p[i+1] or p[i+1] < p[i] < p[i-1]:
        count += 1
print(count)

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

num = 0
for i in range(1, len(p) - 1):
    left = p[i - 1]
    center = p[i]
    right = p[i + 1]

    if (left < center < right) or (right < center < left):
        num += 1

print(num)

Solution (Python):
N = int(input())
p = list(map(int, input().split()))

num = 0
for i, pi in enumerate(p):
    need = 1 <= i <= len(p) - 1 - 1
    if not need: continue
    
    left = p[i - 1]
    center = p[i]
    right = p[i + 1]

    if (left < center < right) or (right < center < left):
        num += 1

print(num)

Solution (Python):
N,*P,=map(int,open(0).read().split())
i=1
ans=0
while i<N-1:
    if P[i-1]<P[i]<P[i+1] or P[i-1]>P[i]>P[i+1]:
        ans+=1
    i+=1
print(ans)

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
c = 0
for i in range(1, n-1):
  b = P[i-1:i+2]
  m = max(b)
  midx = b.index(m)
  b[midx] = 0
  sm = max(b)
  if sm == b[1]:
    c += 1
print(c)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(1,n-1):
  if p[i-1] < p[i] < p[i+1] or p[i-1] > p[i] > p[i+1]:
    ans += 1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(n-2):
  if p[i]<p[i+1]<p[i+2] or p[i]>p[i+1]>p[i+2]: ans+=1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
  if (p[i-1]<=p[i] and p[i]<=p[i+1]) or (p[i+1]<=p[i] and p[i]<=p[i-1]):
    ans+=1
print(ans)

Solution (Python):
a= int(input())
b = input().split()
count = 0
for i in range(a-1):
    if i >= 1:
        if abs(int(b[i-1])-int(b[i])) < abs(int(b[i-1])-int(b[i+1])) and abs(int(b[i+1])-int(b[i])) < abs(int(b[i-1])-int(b[i+1])):
            count += 1
print(count)

Solution (Python):
def q2():
    n = int(input())
    P = [int(i) for i in input().split()]
    ans = 0
    for i in range(1, n-1):
        if (P[i] >= P[i-1] and P[i] <= P[i+1] or
            P[i] <= P[i-1] and P[i] >= P[i+1]):
            ans += 1
    print(ans)

    
if __name__ == '__main__':
    q2()

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(1,n-1):
    s =[p[i-1],p[i],p[i+1]]
    if p[i] == sorted(s)[-2]:
        ans += 1
print(ans)

Solution (Python):
count = 0
n = int(input())
ns = list(map(int, input().split()))
for i in range(n-2):
    l = [ns[i], ns[i+1], ns[i+2]]
    if max(l) != ns[i+1] and min(l) != ns[i+1]:
        count += 1
print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
b = p[0] > p[1]
c = 0
for i in range(1,n-1):
  a = p[i] > p[i+1]
  if a == b:
    c += 1
  b = a
print(c)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1,n -1):
    if p[i - 1] < p[i] < p[i + 1] or p[i - 1] > p[i] > p[i +1]:
        ans += 1
    else:
        pass
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1, n-1):
    if p[i-1]<p[i]<p[i+1] or p[i+1]<p[i]<p[i-1]:
        ans += 1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n-2):
    if (p[i]<=p[i+1]<=p[i+2]) or (p[i]>=p[i+1]>=p[i+2]):
        count += 1
print(count)

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
 
def main():
  n = int(input())
  P = list(map(int,input().split()))
  
  
  
  cnt = 0
  for i in range(1,len(P)-1):
    
    if P[i-1] < P[i] < P[i+1] or P[i-1] > P[i] > P[i+1]:
      cnt += 1
  
  print(cnt)
if __name__ == '__main__':
  main()

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(1,N-1):
  if p[i-1]<=p[i]<=p[i+1] or p[i-1]>=p[i]>=p[i+1]:
    ans += 1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(n - 2):
  if (p[i] < p[i + 1] and p[i + 1] < p[i + 2]) or (p[i] > p[i + 1] and p[i + 1] > p[i + 2]): ans += 1
print(ans)

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
  if max(P[i-1:i+2])!=P[i] and min(P[i-1:i+2])!=P[i]:
    ans+=1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
count=0
for i in range(1,n-1):
  if (p[i-1]>p[i] and p[i]>p[i+1]) or (p[i-1]<p[i] and p[i]<p[i+1]):
    count+=1
print(count)

Solution (Python):
n = int(input())
P = list(map(int, input().split()))

ans = 0
for i in range(n-2):
    x = P[i:i+3]
    if x[1] != min(x) and x[1] != max(x):
        ans += 1

print(ans)

Solution (Python):
n = int(input())

p = list(map(int,input().split()))

count=0

for i in range(n-2):
  if max(p[i],p[i+1],p[i+2])!=p[i+1]:
    if min(p[i],p[i+1],p[i+2])!=p[i+1]:
      count=count+1

print(count)

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]
res = 0
for i in range(1, n - 1):
  if p[i-1] < p[i] < p[i+1] or p[i+1] < p[i] < p[i-1]:
    res += 1
print(res)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
count=0
for i in range(n-2):
  if p[i] < p[i+1] < p[i+2] or p[i+2] < p[i+1] < p[i]:
    count += 1
print(count)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    n = int(input())
    p = input_nums()
    cnt = 0
    for i in range(1, n-1):
        if p[i-1] < p[i] and p[i] < p[i+1]:
            cnt += 1
        elif p[i+1] < p[i] and p[i] < p[i-1]:
            cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
x = 0
count = 0


for a in p:
  if x < n-1:
    if p[x-1] < a < p[x+1] and x != 0:
      count += 1
    elif a < p[x-1] and a > p[x+1] and x != 0:
      count += 1
  x += 1
    

print(count)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
count=0
for i in range(1,n-1):
  s=sorted(p[i-1:i+2])
  if s[1]==p[i]:
    count+=1
    
print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
cnt = 0
for i in range(1,n-1):
  st = [p[x] for x in range(i-1,i+2)]
  st.sort()
  if st[1] == p[i]:
    cnt +=1
print(cnt)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
    if p[i]==sorted([p[i-1],p[i],p[i+1]])[1]: ans+=1
print(ans)

Solution (Python):

import sys


def solve(n: int, p: "List[int]"):
    return sum(1 if p1 <= p2 <= p3 or p3 <= p2 <= p1 else 0 for p1, p2, p3 in zip(p, p[1:], p[2:]))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(n)]  
    print(solve(n, p))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
ans = 0

for i in range(n-2):
  if max(p[i], p[i+1], p[i+2]) != p[i+1] and min(p[i], p[i+1], p[i+2]) != p[i+1]:
    ans = ans + 1

print(ans)

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    p=LIST()
    ct=0
    for i in range(1,n-1):
        if (p[i-1]<=p[i]<=p[i+1]) or (p[i-1]>=p[i]>=p[i+1]):
            ct+=1
    print(ct)
if __name__ == '__main__':
    do()

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
ANS=0
ans=p[0]-p[1]
for i in range(2,N):
    if (p[i-1]-p[i])*ans>=0:
        ANS+=1
    ans=(p[i-1]-p[i])//abs(p[i-1]-p[i])
print(ANS)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
  if max(p[i-1],p[i],p[i+1])!=p[i] and min(p[i-1],p[i],p[i+1])!=p[i]:
    ans+=1
print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
P = iil()
ans = 0
for i in range(1,n-1):
    if (P[i-1] < P[i] < P[i+1]) or (P[i+1] < P[i] < P[i-1]):
        ans += 1
print(ans)

Solution (Python):
n = int(input())
cnt = 0
num = list(map(int, input().split()))
for i in range(n-2):
  serch = sorted([num[i], num[i+1], num[i+2]])
  if serch[1] == num[i+1]:
    cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))

ans = 0
for i in range(n-2):
    if p[i] > p[i+1] > p[i+2] or p[i+2] > p[i+1] > p[i]:
        ans += 1

print(ans)

Solution (Python):
n = int(input())
p = [ int(x) for x in input().split() ]

cnt = 0
for i in range(n-2):
    if sorted(p[i:i+3])[1] == p[i+1]:
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0

for i in range(n-2):
  if p[i+1] == sorted(p[i:i+3])[1]:
    count += 1

print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0
q = []

for i in range(1, n-1):
  q = [p[i-1], p[i], p[i+1]]
  if max(q) != p[i] and min(q) != p[i]:
    count += 1

print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n-2):
    if p[i+1] == sorted(p[i:i+3], reverse = True)[1]:
        count += 1
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
N = 0
for i in range(n - 2):
  l = [p[i], p[i + 1], p[i + 2]]
  if l.index(max(l)) != 1 and l.index(min(l)) != 1:
    N += 1
print(N)

Solution (Python):
n=int(input())
p=list(map(int, input().split()))

count=0
for i in range(1, n-1):
    if p[i-1] < p[i] < p[i+1] or p[i-1] > p[i] > p[i+1]:
        count+=1
print(count)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
cnt = 0
for i in range(n-2):
    if l[i]<l[i+1]<l[i+2] or l[i]>l[i+1]>l[i+2]:
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
cnt = 0
for i in range(n-2):
    if max(l[i], l[i+1], l[i+2]) != l[i+1] and min(l[i], l[i+1], l[i+2]) != l[i+1]:
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
c = 0
for i in range(1, n - 1):
    l = p[i - 1:i + 2]
    l.sort()
    if (l[1] == p[i]):
        c += 1

print(c)

Solution (Python):
n = int(input())
p = [int(i) for i in input().split(" ")]

count = 0
for i in range(1, n-1, 1):
  sub = sorted([p[i-1], p[i], p[i+1]])
  if p[i] == sub[1]:
    count += 1
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
c = 0

for i in range(n - 2):
    if p[i] < p[i + 1] <= p[i + 2] or p[i] >= p[i + 1] > p[i + 2]:
        c = c + 1

print(c)

Solution (Python):
N=int(input())
l=[int(x) for x in input().rstrip().split()]
now=[]
n=[]
cnt=0
for i in range(1,N-1):
  if l[i-1]<l[i] and l[i]<l[i+1]:
    cnt+=1
  elif l[i+1]<l[i] and l[i]<l[i-1]:
    cnt+=1

print(cnt)

Solution (Python):
n=int(input())
p=[0]*n
p=list(map(int,input().split()))

count=0
for i in range(n-2):
  test=sorted([p[i],p[i+1],p[i+2]])
  if (test[1]==p[i+1]):
    count+=1
print(count)

Solution (Python):
n = int(input())
L = list(map(int,input().split()))
cnt =0
for i in range(n-2):
    if (L[i] <= L[i+1] < L[i+2]) or (L[i+2] <= L[i+1] < L[i]):
        cnt +=1
print(cnt)

Solution (Python):

n = int(input())
p = list(map(int, input().split()))
count=0
for i in range(1, n - 1):
    if p[i - 1] < p[i] < p[i + 1] or p[i + 1] < p[i] < p[i - 1]:
        count += 1
print(count)

Solution (Python):
n = int(input())
P = list(map(int,input().split()))
ans = 0
for i in range(1,n-1):
    if P[i-1] < P[i] < P[i+1]:
        ans += 1
    elif P[i-1] > P[i] > P[i+1]:
        ans += 1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(1,n-1):
    mini=min(p[i-1],p[i],p[i+1])
    maxi=max(p[i-1],p[i],p[i+1])
    if p[i]!=mini and p[i]!=maxi:
        ans+=1
print(ans)

Solution (Python):

import sys


def solve(n: int, p: "List[int]"):
    sum = 0
    for i in range(1,n-1):
        a = p[i-1]
        b = p[i]
        c = p[i+1]
        if (a < b and b < c) or (a > b and b > c):
            sum += 1
    print(sum) 
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(n)]  
    solve(n, p)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
ans = 0
for i in range(1, N-1):
    if A[i-1]<A[i]<A[i+1] or A[i-1]>A[i]>A[i+1]:
        ans += 1
print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
m = 0
for i in range(n-2):
  if p[i] < p[i+1] < p[i+2] or p[i+2] < p[i+1] < p[i]:
    m += 1
print(m)

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]

num = 0
for i in range(len(p) - 2):
    n3 = sorted([p[i], p[i + 1], p[i + 2]])
    if n3[1] == p[i + 1]:
        num = num + 1

print(num)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))

count=0
for i in range(1,n-1):
  l=sorted(p[i-1:i+2])
  if l[1] == p[i] and l[0]<l[1]:
    count+=1

print(count)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
cnt = 0
for i in range(1,n-1):
    if(p[i-1]<p[i] and p[i]<p[i+1]):
        cnt += 1
    elif(p[i-1]>p[i] and p[i]>p[i+1]):
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
p_list = list(map(int, input().split()))
ans = 0
for i in range(1, n - 1):
    if p_list[i - 1] < p_list[i] < p_list[i + 1] or p_list[i - 1] > p_list[i] > p_list[i + 1]:
        ans += 1
print((ans))

Solution (Python):

N = int(input())

P_list = list(map(int, input().split()))

counter = 0
for i in range(N-2):
  if((P_list[i]<P_list[i+1]<P_list[i+2])or(P_list[i+2]<P_list[i+1]<P_list[i])):
    counter = counter + 1
    
    
print(counter)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
cnt = 0

for i in range(1, n-1):
    if p[i-1]<p[i]<p[i+1] or p[i+1]<p[i]<p[i-1]:
        cnt += 1

print(cnt)

Solution (Python):
n = int(input())
p = list(map(int,input().split()))

cnt = 0
for i in range(n-2):
    tmp = p[i:i+3]
    ma = max(tmp)
    mi = min(tmp)
    if(tmp[1] != ma and tmp[1] != mi):
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
P = list(map(int, input().split()))

cnt = 0
for i in range(1, n - 1):
    f = 0
    if P[i - 1] < P[i] and P[i] < P[i + 1]:
        f = 1
    elif P[i -1] > P[i] and P[i] > P[i + 1]:
        f = 1
    if f == 1:
        cnt += 1

print(cnt)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(n-2):
  if (p[i]<p[i+1]<p[i+2]) or (p[i]>p[i+1]>p[i+2]):
    ans+=1
print(ans)

Solution (Python):
def main():
    n = int(input())
    p = [int(x) for x in input().split()]
    ans = 0
    for idx in range(n-2):
        if (p[idx] < p[idx+1] and p[idx+1] < p[idx+2]) \
            or (p[idx] > p[idx+1] and p[idx+1] > p[idx+2]):
            ans+=1
    print(ans)

main()

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
count = 0

for i in range(1,n-1):
  if p[i-1] < p[i] < p[i+1] or p[i-1] > p[i] > p[i+1]:
    count += 1
    
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
cnt = 0

for i in range(1, n-1):
    if (p[i-1] < p[i] <= p[i+1]) or (p[i-1] >= p[i] > p[i+1]):
        cnt += 1
print(cnt)

Solution (Python):












n = int(input())
arrP = list(map(int,input().split()))

ans = 0
for i in range(1,n-1):
    if arrP[i-1]<arrP[i]<arrP[i+1] or arrP[i-1]>arrP[i]>arrP[i+1]:
        ans += 1

print(ans)

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
ans=0
for i in range(n-2):
    a=P[i]
    b=P[i+1]
    c=P[i+2]
    if a<b<c or a>b>c:
        ans+=1
print(ans)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
a = 0
for i in range(n-2):
  a += 1-((p[i] < p[i+1])^(p[i+1] < p[i+2]))
print(a)

Solution (Python):
n = int(input())
p = [int(_) for _ in input().split()]
c = 0
for i in range(1, n-1):
    if p[i+1] > p[i] and p[i] > p[i-1]:
        c += 1
    elif p[i-1] > p[i] and p[i] > p[i+1]:
        c += 1
print(c)

Solution (Python):
n = int(input())

p_list = list(map(int, input().split()))

ans = 0
for i in range(n-2):
    tmp = sorted(p_list[i:i+3], reverse=True)[1]
    if tmp == p_list[i+1]:
        ans += 1

print(ans)

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
sum=0
for i in range(a-2):
  b=l[i]
  c=l[i+1]
  d=l[i+2]
  if c!=min(b,c,d) and c!=max(b,c,d):
    sum +=1
print(sum)

Solution (Python):
import math
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))
mod = 10**9 + 7 
sys.setrecursionlimit(1000010)

n = ni()
p  = nl()
ans = 0
for i in range(1,n-1):
    if p[i-1] < p[i] < p[i+1]:
        ans += 1
    elif p[i-1] > p[i] > p[i+1]:
        ans += 1

print(ans)

Solution (Python):

n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(1,n-1):
    if (p[i+1] < p[i] < p[i-1] or p[i+1] > p[i] > p[i-1]):
        ans += 1
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
a=0
for i in range(n-2):
  p,q,r=l[i:i+3]
  if p<q<r or r<q<p: a+=1
print(a)

Solution (Python):
n = input()
p = map(int,raw_input().split())
res = 0
for i in xrange(0,n-2):
    nl = [p[i],p[i+1],p[i+2]]
    
    if sorted(nl)[1] == nl[1]:
        res +=1
print res

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
a = 0
for i in range(n-2):
  if (p[i]<p[i+1] and p[i+1] < p[i+2]) or (p[i]>p[i+1] and p[i+1] > p[i+2]):
    a += 1
print(a)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ans = 0
for i in range(n-2):
    b = a[i:i+3]
    c = b[1]
    b.sort()
    if c == b[1]:
        ans += 1

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
cnt = 0
for i in range(2, n):
    if min(p[i - 2], p[i - 1], p[i]) != p[i - 1] and max(p[i - 2], p[i - 1], p[i]) != p[i - 1]:
        cnt += 1
    
print(cnt)

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
count=0
for i in range(1,len(p)-1):
    l=[]
    l.append(p[i-1])
    l.append(p[i])
    l.append(p[i+1])
    l.sort()
    if l[1]==p[i]:
        count=count+1
print(count)

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    c = 0
    for i in range(1, N-1):
        if A[i-1] < A[i] and A[i] < A[i+1] or A[i-1] > A[i] and A[i] > A[i+1]:
            c += 1
    return c

print(solve())

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
cnt=0
for i in range(n-2):
    if P[i]<P[i+1]<P[i+2] or P[i+2]<P[i+1]<P[i]:
        cnt+=1
print(cnt)