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):
n = int(input())
P = list(map(int, input().split()))

cnt = 0
for i in range(1, len(P)-1):
    sub = [P[i-1], P[i], P[i+1]] 
    sub.sort()
    if P[i] == sub[1]:
        cnt += 1

print(cnt)

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

count = 0

for i in range(N-2):
    first = A[i]
    second = A[i+1]
    third = A[i+2]
    if first < second:
        if second < third:
            count += 1
        else:
            count += 0
    else:
        if second > third:
            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):

import sys


def solve(n: int, p: "List[int]"):
    counter = 0
    for i in range(1, n-1):
        if (p[i-1] - p[i]) * (p[i] - p[i+1]) > 0:
            counter += 1
    print(counter)
    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())
p = list(map(int, input().split()))
ans = sum(1 for i in range(n-2) if p[i] < p[i+1] < p[i+2] or p[i] > p[i+1] > p[i+2])
print(ans)

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


counter = 0
for i in range(1, n-1):
    buff = []
    buff = P[i-1:i+2]
    buff.sort()
    if P[i] == buff[1]:
        counter += 1


print(counter)

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

Solution (Python):
def main():
    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]:
            count += 1
        if p[i-1] > p[i] and p[i] > p[i+1]:
            count += 1
    print(count)
    
if __name__ == '__main__':
    main()

Solution (Python):
import math

def main():
    n = int(input())
    order_list = list(map(int, input().split(" ")))
    count = 0
    for i in range(n-2):
        dif1 = order_list[i+1]-order_list[i]
        dif2 = order_list[i+2]-order_list[i+1]
        if dif1*dif2 > 0:
            count = count + 1
        else:
            pass

    print(count)

if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
c=0
for i in range(n-2):
    a=p[i:i+3]
    b=sorted(a)
    if a[1]==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):
def main05():
    num = int(input())
    data = input().split()
    zyun = []
    for i in data:
        zyun.append(int(i))
    result = 0
    for i in range(num-2):
        if zyun[i] < zyun[i+1]:
            if zyun[i+1] < zyun[i+2]:
                result = result + 1
        else:
            if zyun[i+1] > zyun[i+2]:
                result = result + 1
    print(result)

if __name__ == "__main__":
    main05()

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

ans = 0
for i in range(1, n-1):
    if p[i] != max(p[i-1], p[i], p[i+1]) and p[i] != min(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):
    a=[p[i-1],p[i],p[i+1]]
    if sorted(a)[1]==p[i]:
        cnt+=1
print(cnt)

Solution (Python):
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

print(cnt)

Solution (Python):
n=int(input())
L=list(input().split())
t=1
Q=0
while t<n-1:
  if int(L[t-1])<int(L[t])<int(L[t+1]) or int(L[t+1])<int(L[t])<int(L[t-1]):
    Q+=1
  t+=1
print(Q)

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]:
        ans = ans + 1
    if p[i - 1] > p[i] and p[i] > p[i + 1]:
        ans = ans + 1
print(ans)

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

ans = 0
for a, b, c in zip(p[:-2], p[1:-1], p[2:]):
    if a < b < c or c < b < a:
        ans += 1

print(ans)

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

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

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

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

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

Solution (Python):
n = int(input())
p_li = list(map(int,input().split()))
cnt = 0
for i in range(n):
    if i == 0 :
        continue
    if i == n - 1 :
        break
    original = p_li[i]
    p_i_li = [p_li[i - 1], p_li[i], p_li[i + 1]]
    p_i_li.sort()
    if p_i_li[1] == original :
        cnt += 1
print(cnt)

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

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

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

for i in range(1,n-1):
  
  if  (A[i]-A[i-1])*(A[i+1]-A[i]) > 0:
    count += 1

print(count)

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):
def checker(i,s):
  if s[i] > s[i+1] and s[i+1] > s[i+2]:
    return True
  elif s[i] < s[i+1] and s[i+1] < s[i+2]:
    return True
  return False

n = int(input())
s = [int(i) for i in input().split()]
res = 0

for i in range(n-2):
  if checker(i,s):
    res += 1
print(res)

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

cnt = 0
l = len(p)

for i in range(l):
    if i == 0 or i == l - 1:
        continue

    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(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())
a=[int(i) for i in input().split()]
ans=0
for i in range(1,len(a)-1):
    if((a[i]>a[i-1] and a[i]<a[i+1]) or (a[i]<a[i-1] and a[i]>a[i+1])):
        ans+=1
print(ans)

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

Solution (Python):
import io
import sys


def IN_S():
  return input()


def IN_I():
  return int(input())


def IN_L_I():
  return list(map(int, input().split()))


def IN_L_S():
  return list(map(str, input().split()))


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
5
1 3 5 4 2
'''


def MAIN():
  
  A()


def A():
  s = IN_I()
  l = IN_L_I()

  r = [0] * s

  p = l[0]
  pc = 0
  m = l[0]
  mc = 0

  for num in l[1:]:
    if num < p:
      pc += 1
    else:
      r[pc] += 1
      pc = 0

    if num > m:
      mc += 1
    else:
      r[mc] += 1
      mc = 0

    p = num
    m = num

  r[pc] += 1
  r[mc] += 1

  ret = 0
  for i, count in enumerate(r[2:]):
    ret += (i + 1) * count

  print(ret)

def B():
  return None


def C():
  return None


MAIN()

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=[int(x) for x in 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=count+1
print(count)

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

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

ans = 0
for i in range(len(X) - 2):
  if (X[i] > X[i+1] > X[i+2]) or (X[i] < X[i+1] < X[i+2]):
  	ans += 1

print(ans)

Solution (Python):
n = int(input())
target = list(map(int, input().split()))
ans = 0
for i in range(n-2):
  if sorted([target[i], target[i+1], target[i+2]])[1] == target[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] and p[i] < p[i+1]) or (p[i-1] > p[i] and 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,len(p)-1):
	if p[i-1] <= p[i] and p[i] <= p[i+1]:
		ans += 1
	elif p[i-1] >= p[i] and p[i] >= p[i+1]:
		ans += 1

print(ans)

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

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

result=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]:
    result +=1
print(result)

Solution (Python):
A = int(input())
B = list(map(int, input().split()))
ans = 0

for i in range(1, A-1):
  if B[i-1] > B[i] > B[i+1]:
    ans += 1
  elif B[i-1] < B[i] < B[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]>p[i-1]:
        if p[i+1]>p[i]:
            count=count+1
    elif p[i]>p[i+1]:
        if p[i-1]>p[i]:
            count+=1
print(count)

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

Solution (Python):
n=int(input())
t=list(map(int,input().split()))
ans=0
a=[]
for i in range(n-2):
    a.append(t[i])
    a.append(t[i+1])
    a.append(t[i+2])
    total=sum(a)
    total=total-min(a)-max(a)
    if total==a[1]:
        ans+=1
    del a[:]
print(ans)

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

Solution (Python):


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

count = 0


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

print(count)

Solution (Python):
n = int(input())
p = input().rstrip().split(' ')
count = 0
for i in range(n-2):
  buf = []
  buf.append(int(p[i]))
  buf.append(int(p[i+1]))
  buf.append(int(p[i+2]))
  buf.sort()
  if int(p[i+1]) == buf[1]:
    count = count + 1
print(count)

Solution (Python):
import math
num = input()
x =  [int(s) for s in input().split()]
sum = 0
for i in range(len(x)-2):
    center = x[i+1]
    three = [x[i], x[i+1], x[i+2]]
    if(x[i+1] == int(sorted(three)[1])):
        sum += 1
        
print(sum)

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

n=n.split()
n=int(n[0])
p=p.split()
p=[int(p) for p in p]

output=0
for i in range(n-2):
    if p[i]<p[i+1] and p[i+1]<p[i+2]:
        output=output+1
    elif p[i+2]<p[i+1] and p[i+1]<p[i]:
        output=output+1
    else:
        output=output+0
    
print(output)

Solution (Python):



n = int(input())




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




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())
li = list(map(int,input().split()))
count=0
for i in range(n-2):
  if(li[i+2]>li[i+1] and li[i+1]>li[i]):
    count+=1
  if(li[i+2]<li[i+1] and li[i+1]<li[i]):
    count+=1

print(count)

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

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

for i in range ( 1, len(a)-1):
  if (a[i-1]<a[i]<a[i+1]) or (a[i+1]<a[i]<a[i-1]):
    cnt += 1
    
print (cnt)

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

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

print(s)

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

print(total)

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

count = 0
for i in range(1, len(li)-1):
    if (li[i] > li[i+1] and li[i] < li[i-1] )or (li[i] > li[i-1] and li[i] < li[i+1] ):
        count+= 1

print(count)

Solution (Python):

n = int(input())
l = list(map(int,input().split()))
ans = 0

for i in range(1,n-1):
    if l[i-1] < l[i] < l[i+1] or l[i-1] > l[i] > l[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] and p[i] < p[i+1]:
        count += 1
    elif 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()))
c = 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]):
        c += 1
print(c)

Solution (Python):
n = int(input())
p = [int(x) for x in input().split()]
count = 0
for i in range(1,n-1):
    count = count + (1 if (float(p[i]-p[i+1])+0.1) * (float(p[i]-p[i-1])+0.1) < 0 else 0 )
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] and p[i] <= p[i+1]) or (p[i+1] < p[i] and p[i] <= p[i-1]):
        ans = ans+1
print(ans)

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

Solution (Python):
from fractions import gcd
import math
def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft



def main():
    n = int(input())
    a=list(map(int, input().split()))
    ans = 0
    for i in range(n-2):
        if a[i] <= a[i+1] < a[i+2]:
            ans += 1
        if a[i+2] <= a[i+1] < a[i]:
            ans += 1
    print(ans)
        
main()

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

for i in range(1,n-1):
  if (p[i]-p[i+1]) * (p[i]-p[i-1]) < 0:
    c += 1

print(c)

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

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

print(count)

Solution (Python):




def count_ordinary_num(n, arr):
    cnt = 0
    for i in range(1, n - 1):
        if (arr[i - 1] < arr[i] < arr[i + 1]) or (arr[i - 1] > arr[i] >
                                                  arr[i + 1]):
            cnt += 1
    return cnt


def main():
    n = int(input())
    p_arr = list(map(int, input().split()))

    ans = count_ordinary_num(n, p_arr)
    print(ans)


main()

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

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

count=0
for i in range(n-2):
    a=p[i:i+3]
    if max(a)!=p[i+1] and min(a)!=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):
  if i == 0 or i == n-1:
    continue
  elif p[i-1] < p[i] and p[i+1] > p[i]:
    ans += 1
  elif p[i-1] > p[i] and p[i+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):
    a = p[i-1 : i+2]
    a.sort()
    if a[1] == p[i]:
        count += 1
print(count)

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

p = list(int(i) for i in input().split())


ans = 0
for i in range(1, n - 1):
    l = [p[i - 1], p[i], p[i + 1]]
    l.sort()
    if (l[1] == p[i]):
        ans += 1

print(ans)

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

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

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

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

c = 0
for i,j,k in zip(pList[0:], pList[1:], pList[2:]):
    x = [i,j,k]
    x.sort()
    if x[1] == j:
        c += 1
print(c)

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

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

print(count)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
k = 0
for i in range(1,n-1):
  p_1 = l[i-1]
  p_2 = l[i]
  p_3 = l[i+1]
  _ = [p_1,p_2,p_3]
  if (min(_)==p_1 and max(_)==p_3) or (min(_)==p_3 and max(_)==p_1):
    k +=1
print(k)

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

Solution (Python):
n = int(input())
p = [int(i) for i in 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):
n = int(input())
p = list(map(int, input().split()))

count = 0

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

print(count)

Solution (Python):
n = int(input())
p = list(map(lambda x: int(x), input().split(" ")))
c = 0
for i in range(1, n-1):
    a = max(p[i-1:i+2])
    b = min(p[i-1:i+2])
    if a is not p[i] and b is not p[i]:
        c = c + 1
print(c)

Solution (Python):
n = int(input())
pl = list(map(int, input().split()))
count = 0
for i, p in enumerate(pl[1:-1]):
    if sorted((p, pl[i], pl[i + 2]))[1] == p:
        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]) or (P[i-1]>P[i] and 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(n-2):
    if p[i]<p[i+1] and p[i+1]<p[i+2]:
        count+=1
    if p[i]>p[i+1] and p[i+1]>p[i+2]:
        count+=1
print(count)

Solution (Python):



def main():
    n = int(input())
    p = list(map(int, input().split()))
    count = 0

    for i in range(1, n - 1):
        value_max = max(p[i - 1], p[i], p[i + 1])
        value_min = min(p[i - 1], p[i], p[i + 1])

        if p[i] != value_max and p[i] != value_min:
            count += 1

    print(count)


if __name__ == '__main__':
    main()

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

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

print(count)

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

for i in range(1,n-1):
  l=sorted([p[i-1],p[i],p[i+1]])
  if l[1]==p[i]:
    a+=1

print(a)

Solution (Python):

INPUT_SENTENCES = 2
ss = [input() for i in range(INPUT_SENTENCES)]
n = int(ss[0])
p = [int(i) for i in ss[1].split()]
result = 0
print(len(
    [1 for i in
        [{"pm": sorted([p[i-1], p[i], p[i+1]])[1], "p": p[i]} for i in range(1, n-1)]
        if i["pm"] == i["p"]]
        ))

Solution (Python):

INPUT_SENTENCES = 2
ss = [input() for i in range(INPUT_SENTENCES)]
n = int(ss[0])
p = [int(i) for i in ss[1].split()]
result = 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]:
      result += 1
print(result)

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

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

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

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

Solution (Python):
import numpy as np

N = int(input())
data = list(map(int, input().split()))
pn = [x for x in range(1, N-1)]

cnt = 0
for p in pn:
    p_t = np.sort([data[p - 1], data[p], data[p + 1]])
    cnt = cnt + 1 if p_t[1] == data[p] else cnt
    
print(cnt)

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

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

print(count)

Solution (Python):


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

print(ans)

Solution (Python):
import numpy as np

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

cnt = 0
for i in np.arange(1,n-1):
    tmp_l = [P[i-1],P[i],P[i+1]]
    tmp_l.sort()
    if tmp_l[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-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()))
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(map(int,input().split()))
flag=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]:
  flag+=1
print(flag)

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]):
    ans += 1
  elif (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(n):
    if i == 0 or i == n-1:
        ans += 0
    elif (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):
N = input()
n = map(int, raw_input().split())

ans = 0

for i in range(1, len(n) - 1):
  if n[i] < n[i - 1] and n[i] > n[i + 1]:
    ans += 1
  if n[i] > n[i - 1] and n[i] < n[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]:
        cnt += 1
    elif p[i-1]>p[i] and p[i]>p[i+1]:
        cnt += 1
    else:
        cnt = cnt
print(cnt)

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
sum = 0
for i in range(n-2):
    if ((p[i+2] < p[i+1]) & (p[i+1] < p[i])) | ((p[i] < p[i+1]) & (p[i+1] < p[i+2])):
        sum = sum+1
print(sum)

Solution (Python):
def myInput():
  n = input()
  s = input()
  return (n, [int(i) for i in s.split()])

def func():
  n, lst1 = myInput()
  cnt = 0
  for i in range(1, int(n)-1):
    if min(lst1[i+1],max(lst1[i], lst1[i-1])) == lst1[i]:
      cnt+=1
    elif min(lst1[i-1],max(lst1[i],lst1[i+1])) == lst1[i]:
      cnt += 1
  return cnt
print(func())

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

K=0

for i in range(N-2):
    if list1[i]>list1[i+2]:
        if list1[i]>list1[i+1] and list1[i+1]>list1[i+2]:
            K+=1
            
    else:
        if list1[i]<list1[i+1] and list1[i+1]<list1[i+2]:
            K+=1

print(K)

Solution (Python):
n=int(input())
p=[int(i) for i in 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):

import sys


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

ans = 0
for i in range(N - 2):
    pool = [p[i], p[i + 1], p[i + 2]]
    if min(pool) == p[i + 1]:
        continue
    if max(pool) == p[i + 1]:
        continue
    
    ans += 1

print(ans)

Solution (Python):
n=int(input())
b=list(map(int,input().split()))
c=0
d=0
while c<=n-3 :
    if (b[c+1]>b[c] and b[c+1]<b[c+2]) or (b[c+1]<b[c] and b[c+1]>b[c+2]):
        d +=1
        c +=1
    else:
        c+=1

print(d)

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

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

print(count)

Solution (Python):
n = int(input())
p_ = input().split(" ")


def compare(p, i):
    if p[i-1] < p[i] & p[i] < p[i+1]:
        return True
    if p[i-1] > p[i] & p[i] > p[i + 1]:
        return True
    return False
dic = {}
p = []

for i in range(n):
    p.append( int(p_[i]))

count=0
for i in range(1,n-1):
  res = compare(p,i)
  if res:
    count+=1

print(count)

Solution (Python):
n = int(input())
p_list = input().split(' ')

p_i_0 = int(p_list[0])
p_i_1 = int(p_list[1])
p_i_2 = int(p_list[2])

counter = 0

for i in range(n - 3):
    if ((p_i_0 - p_i_1) * (p_i_2 - p_i_1) <= 0):
        counter += 1
    p_i_0 = p_i_1
    p_i_1 = p_i_2
    p_i_2 = int(p_list[i + 3])

if ((p_i_0 - p_i_1) * (p_i_2 - p_i_1) <= 0):
    counter += 1
print(counter)

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    nums = string_to_int(inputs[0])
    count = 0
    for i in range(0, len(nums)-2):
        if nums[i+1] >= nums[i] and nums[i+1] <= nums[i+2]:
            count += 1
        elif nums[i+1] <= nums[i] and nums[i+1] >= nums[i+2]:
            count += 1
    return count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

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

count = 0

for i in range(1,len(p)-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):
n = int(input())

ls = list(map(int, input().split()))
 
count = 0

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

print(str(count))

Solution (Python):
input()
a = list(map(int, input().split()))
print(sum([sorted(a[i:i+3])[1] == a[i+1] for i in range(len(a)-2)]))

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

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

print(str(count))

Solution (Python):
import statistics as stat

def calc(p):
  count = 0
  for i in range(1, len(p) - 1):
    if p[i] == stat.median(p[i - 1:i + 2]):
      count += 1
  return count

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

print(calc(p))

Solution (Python):
A = int(input())
C = list(map(int, input().split()))
cnt = 0
for i in range(A-2):
    q = 0
    w = 0
    e = 0
    q = C[i]
    w = C[i + 1]
    e = C[i + 2]
    if q < w < e or e < w < q:
        cnt += 1

print(cnt)

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+1]:
        a = True if p[i] > p[i-1] and p[i] < p[i+1] else False
    elif p[i-1] > p[i+1]:
        a = True if p[i] < p[i-1] and p[i] > p[i+1] else False
    if a:
        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] and p[i+1]>p[i+2]):
        ans += 1
    if(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(n-2):
    if(p[i]>p[i+1] and p[i+1]>p[i+2]):
        Ans+=1
    if(p[i]<p[i+1] and p[i+1]<p[i+2]):
        Ans+=1

print(Ans)

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

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


print(count)