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):


import sys
def input(): return sys.stdin.readline().strip()
def gcd(x, y): return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y): return (x * y) // gcd(x, y)

N = int(input())
P = [int(x) for x in input().split()]




ans = 0
for i in range(N - 2):
    p = P[i : i + 3]
    p.sort()

    if p[1] == P[i + 1]:
        ans += 1

print(ans)

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

ans = 0

for i in range(2, n):
    if (p[i-2] > p[i-1] and p[i-1] > p[i]) or (p[i-2] < p[i-1] 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(1, len(p) - 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):
n = int(input())
P = list(map(int,input().split()))
cnt = 0
for i in range(1,n-1):
    s = P[i-1]+P[i]+P[i+1]
    if s-min(P[i-1],P[i],P[i+1])-max(P[i-1],P[i],P[i+1]) == P[i]:
        cnt += 1
print(cnt)

Solution (Python):
n=int(input())
p_list=list(map(int,input().split()))
count=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]:
        count+=1
print(count)

Solution (Python):


def main():
    n = int(input())
    ps = list(map(int, input().split()))
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

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())
ps = list(map(int, input().split()))
c = 0
for p1, p2, p3 in zip(ps, ps[1:], ps[2:]):
    if sorted([p1, p2, p3])[1] == p2:
        c += 1
print(c)

Solution (Python):
def main():
     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)
          
if __name__=='__main__':
     main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)



def solve():
    N = int(input())
    p_list = list(map(int, input().split()))

    cnt = 0
    for i in range(1, N-1):
        pre = p_list[i-1]
        p = p_list[i]
        pos = p_list[i+1]

        if p < pre and p > pos:
            cnt += 1
        elif p > pre and p < pos:
            cnt += 1

    print(cnt)

solve()

Solution (Python):

from collections import defaultdict
import math
import re

def main():
    
    N = int(input().strip())
    P = list(map(int, input().split()))

    cnt = 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]):
            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):
    ps = p[i-1:i+2]
    ps.sort()
    if p[i] == ps[1]:
        ans += 1

print(ans)

Solution (Python):
import sys
from collections import deque

readline = sys.stdin.readline

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

a = len([1 for i in range(1, n - 1) if 
    (p[i] < p[i + 1] or p[i] < p[i - 1]) and
    (p[i] > p[i + 1] or p[i] > p[i - 1])
])

print(a)

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

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

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
cnt = 0
for i in range(1,n-1):
    tmp= [p[i-1],p[i],p[i+1]]
    if tmp[1]>=tmp[0] and tmp[1]<=tmp[2]:
        cnt += 1
    elif tmp[1]>tmp[2] and tmp[1]<=tmp[0]:
        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] != max(p[i-1], p[i], p[i+1]) and p[i] != min(p[i-1], 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(1, n - 1):
    p = P[i - 1 : i + 2]
    p.sort()
    if len(set(p)) > 1 and p.index(P[i]) == 1:
        ans += 1

print(ans)

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

    cnt = 0

    for p1,p2,p3 in zip(pp,pp[1:],pp[2:]):
        if p1 > p2 >= p3 or p1 <= p2 < p3:
            cnt += 1

    print(cnt)
    
resolve()

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

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

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

print(ans)

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

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

Solution (Python):

import sys


def solve(n: int, p: "List[int]"):
    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)
    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()))

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

Solution (Python):
import sys

fin = sys.stdin.readline
n = int(fin())
p_list = [int(elem) for elem in fin().split()]

count = 0
for i in range(1, len(p_list)-1):
    prev_p = p_list[i-1]
    next_p = p_list[i+1]
    cur_p = p_list[i]
    if prev_p < cur_p < next_p:
        count += 1
    elif prev_p > cur_p > next_p:
        count += 1
print(count)

Solution (Python):
N =int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(N-2):
  a = A[i:i+3]
  b = sorted(A[i:i+3])
  if a[1] == b[1]:
    cnt += 1
    
print(cnt)

Solution (Python):
n = int(input())
p_list = [int(x) for x in input().split()]
cnt = 0
for i in range(n-2):
    cnt += ((p_list[i] < p_list[i+1] < p_list[i+2]) or (p_list[i] > p_list[i+1] > p_list[i+2]))
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]:
        cnt+=1
    elif p[i]>p[i+1]>p[i+2]:
        cnt+=1
print(cnt)

Solution (Python):

import sys


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

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(n-2):
    if p[i]<p[i+1] and p[i+2]>p[i+1]:
        ans+=1
    elif p[i]>p[i+1] and p[i+2]<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):
    p1 = p[i-1]
    p2 = p[i]
    p3 = p[i+1]
    if (p2 >= p1 and p3 >= p2) or (p2 >= p3 and p1 >= p2):
        count += 1

print(count)

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] and p[i+1]<p[i+2]:
        cnt+=1
    elif p[i]>p[i+1] and p[i+1]>p[i+2]:
        cnt+=1
    else:
        continue

print(cnt)

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

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

Solution (Python):


import sys

class BOrdinaryNumber:
    def solve(self):
        n = int(input())
        p = [int(_) for _ in input().split()]
        ans = 0
        for i in range(1, n - 1):
            if min(p[i - 1], p[i + 1]) < p[i] < max(p[i - 1], p[i + 1]):
                ans += 1
        
        print(ans)

solver = BOrdinaryNumber()
input = sys.stdin.readline

solver.solve()

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

count=0
for i in range(N-2):
    if x[i]<=x[i+1] and x[i+1]<=x[i+2]:
        count+=1
    elif x[i]>=x[i+1] and x[i+1]>=x[i+2]:
        count+=1
print(count)

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]

cnt = 0
for i in range(1, N - 1):
    A = [P[i - 1], P[i], P[i + 1]]
    maxA = max(A)
    minA = min(A)
    if P[i] != maxA and P[i] != minA:
        cnt += 1
        
print(cnt)

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

print(ans)

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

print(count)

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

Solution (Python):
n=int(input())
P=[int(i) for i in input().split()]
ans=0
for i in range(2,n):
  if P[i-2]<P[i-1]<=P[i] or P[i]<P[i-1]<=P[i-2]:
    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):

import sys


def solve(n: int, p: "List[int]"):
    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)
    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):
import sys
input = sys.stdin.readline
ri = lambda: int(input())


rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

n=ri()
p=list(rl())
ans = 0
for i in range(1,n-1):
    p1,p2,p3 = p[i-1],p[i],p[i+1]
    p123 = [p1,p2,p3]
    if p2 != max(p123) and p2 != min(p123):
        ans += 1

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
c = 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]:
        c += 1
print(c)

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

answer = 0
for i in range(len(p)-2):
	if p[i+1] > min(p[i], p[i+2]) and p[i+1] < max(p[i], p[i+2]):
		answer += 1

print(answer)

Solution (Python):

input()

x=list(map(int,input().split(' ')))

count=0

for i in range(1,len(x)-1):
    temp=sorted(x[i-1:i+2],reverse=True)
    if temp[1]==x[i]:
        count+=1

print(count)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split(" ")]
flag = False
cnt = 0
for i in range(1,N-1):
    if A[i-1]>A[i]:
        if A[i+1]<A[i]:
            flag = True
    else:
        if A[i+1]>A[i]:
            flag = True
    if flag:
        cnt+=1
        flag = False
print(cnt)

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

n = int(input())
p = list(map(int, input().split()))
counter = 0
for i in range(1, n-1):
    lis = (p[i-1], p[i], p[i+1])
    mid = statistics.median(lis)
    if p[i] == mid:
        counter += 1

print(counter)

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

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

print(cnt)

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

p_list = P[:]

ans = 0
for a, b, c in zip(p_list[0:-2],p_list[1:-1],  p_list[2:]):
    x = [a, b, c]
    x = sorted(x)
    if x[1] == b:
        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+2] < p[i+1] < p[i]:
    ans += 1
print(ans)

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

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

cnt = 0
for i, pi in enumerate(p[:-2]):
  if p[i] < p[i+1] and p[i+1] == p[i+2]: cnt += 1
  elif p[i] < p[i+1] and p[i+1] < p[i+2]: cnt += 1
  elif p[i+2] < p[i+1] and p[i+1] == p[i]: cnt += 1
  elif p[i+2] < p[i+1] and p[i+1] < p[i]: cnt += 1

print(cnt)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
count = 0
for i in range(len(l) - 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]) :
        count += 1
print(count)

Solution (Python):

def solve(N, P):
    c = 0
    for i in range(1,N-1):
        if P[i-1] < P[i] < P[i+1]:
            c += 1
        if P[i+1] < P[i] < P[i-1]:
            c += 1
    return c

N = int(input())
P = [int(x) for x in input().split()]
print(solve(N, P))

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
a=1
c=0
while True:
    f=0
    x=p[a-1]
    y=p[a]
    z=p[a+1]
    if y>x and z>y:
        f=1
    if y>z and x>y:
        f=1
    if f==1:
        c=c+1
    a=a+1
    if a==n-1:
        break
print(c)

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 += 1

print(count)

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

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

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

print(ans)

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
ans = 0
for x,y,z in zip(p,p[1:],p[2:]):
    ls = []
    ls.append(x)
    ls.append(y)
    ls.append(z)
    ls.sort()
    if ls[1] == y:
        ans += 1
print(ans)

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

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

print(cnt)

Solution (Python):



import sys  
import os
import time
import re
from pydoc import help
import string
import math
from operator import itemgetter
from collections import Counter
from collections import deque
from collections import defaultdict as dd
import fractions
from heapq import heappop, heappush, heapify
import array
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy as dcopy
import itertools



sys.setrecursionlimit(10**7)
INF = 10**20
GOSA = 1.0 / 10**10
MOD = 10**9+7
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)]  
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def DP(N, M, first): return [[first] * M for n in range(N)]
def DP3(N, M, L, first): return [[[first] * L for n in range(M)] for _ in range(N)]
from inspect import currentframe


def local_input():
    from pcm.utils import set_stdin
    import sys
    from pathlib import Path
    parentdir = Path(os.path.dirname(__file__)).parent
    inputfile = parentdir.joinpath('test/sample-1.in')
    if len(sys.argv) == 1:
        set_stdin(inputfile)


def solve():
    n = int(input())
    l = list(map(int,input().split()))
    ans = 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]:
            ans += 1
    print(ans)
    return 0

if __name__ == "__main__":
    try:
        local_input()
        def dump(*args):
            names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
            print(', '.join(names.get(id(arg),'???')+' => '+repr(arg) for arg in args), file=sys.stderr)
    except:
        def dump(*args):
            pass

    solve()

Solution (Python):
n = int(input())
ps = list(map(int, input().split()))
cnt = 0
for i in range(n-2):
    p0 = ps[i]
    p1 = ps[i+1]
    p2 = ps[i+2]
    if p0 < p1 and p1 < p2:
        cnt += 1
    elif p2 < p1 and p1 < p0:
        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] > p[i+1] > p[i+2]:
        cnt += 1

print(cnt)

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

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

cnt = 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]):
        cnt += 1

print(cnt)

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

ans = 0

for i in range(1, n-1):
  if ps[i-1] < ps[i] < ps[i+1]:
    ans += 1
  elif ps[i-1] > ps[i] > ps[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):
  A = P[i-1:i+2]
  
  if max(A) > P[i] and min(A) < P[i]:
    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]<=P[i+1] or P[i+1]<=P[i]<=P[i-1]:
        cnt += 1

print(cnt)

Solution (Python):

"""
Created on Sat Jun 29 20:53:06 2019

@author: Yamazaki Kenichi
"""

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

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

print(ans)

Solution (Python):
n = int(input())
lm = list(map(int,input().split()))
s = 0
for x in range(1,n-1):
    if (lm[x] > lm[x+1] and lm[x]<lm[x-1]) or(lm[x] > lm[x-1] and lm[x]<lm[x+1]):
        s += 1
    else:
        continue
print('{}'.format(s))

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

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

Solution (Python):
n=input()
P=map(int, raw_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(1,n-1):
	if sorted([P[i-1],P[i],P[i+1]])[1]==P[i]:
		cnt+=1
print(cnt)

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

cnt = 0
for i in range(1, len(P) - 1):
    if P[i] == sorted(P[i-1:i+2])[1]:
        cnt += 1

print(cnt)

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] != 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()))
counter = 0
for i in range(1,n-1):
    if p[i-1] < p[i] and p[i] < p[i+1]:
        counter += 1
    elif p[i-1] > p[i] and p[i] > p[i+1]:
        counter += 1
print(counter)

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+2] < p[i+1] 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):
    a, b, c = p[i], p[i + 1], p[i + 2]
    if (b > a and b <= c) or (b <= a and 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 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]!=max(p[i-1:i+2]) and p[i]!=min(p[i-1:i+2]):
        ans+=1
print(ans)

Solution (Python):
N = int(input())
P = list(map(int,input().split(" ")))
c=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]:
        c+=1
print(c)

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

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

print(count)

Solution (Python):
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

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

cnt = 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]):
        cnt += 1

print(cnt)

Solution (Python):
def main():
    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)


main()

Solution (Python):
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy




def main():
    N = int(input())
    P = list(map(int, input().split()))
    count = 0
    for i in range(N-2):
        a = sorted(P[i:i+3])
        if a[1] == P[i+1]:
            count += 1 
    print(count)

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(input().split())

p = [int(p) for p in p_]

res = 0

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

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):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
p = readl()
k = 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]):
        k += 1
print(k)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())
p = get_nums_l()

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

print(ans)

Solution (Python):
def main():
    _ = int(input())
    p = [int(s) for s in input().split()]
    print(solve(p))

def solve(p):
    cnt = 0
    for a, b, c in zip(p, p[1:], p[2:]):
        if (a < b < c) or (c < b < a):
            cnt += 1
    return cnt

main()

Solution (Python):
n = int(input())
ps = [int(i) for i in input().split()]
print(sum([1 for i in range(1, n-1) if not((ps[i-1] < ps[i] and ps[i+1] < ps[i]) or (ps[i-1] > ps[i] and ps[i+1] > ps[i]))]))

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

count = 0
for i in range(n-2):
    if p[i] < p[i+1] <= p[i+2]:
        count += 1
    elif p[i] >= p[i+1] > p[i+2]:
        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]- P[i]) < 0:
        cnt += 1

print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ret = 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]:
        ret += 1
print(ret)

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

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

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

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

ans = 0

for i in range(n-2):
    p1 = p_list[i]
    p2 = p_list[i + 1]
    p3 = p_list[i + 2]
    if min(p1, p2, p3) != p2 and max(p1, p2, p3) != p2:
        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]:
        count += 1
    elif p[i-1] > p[i] > p[i+1]:
        count+= 1
print(count)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
result = 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:
        result += 1
print(result)

Solution (Python):
n=int(input())
p=list(map(int,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()))

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

print(cnt)

Solution (Python):
n = int(input())
pList = list(map(int,input().split()))
c = 0
for i in range(1,n-1):
    tList = [pList[i-1],pList[i],pList[i+1]]
    tList.sort()
    if pList[i] == tList[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] > 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)

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
a=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]:
        a+=1
print(a)

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

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 

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

n = int(input())
p = LI()
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()))
ans = 0
for i in range(1,n-1):
    if p[i-1] > p[i] and p[i] > p[i+1]:
        ans += 1
    if p[i-1] < p[i] and p[i] < p[i+1]:
        ans += 1
print(ans)