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



Sample input:
5
1 3 5 4 2


Sample output:
2

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x 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]>p[i+1] and p[i+1]>p[i+2]:
    ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(c)

Submission status: Accepted

Solution code:
n = int(input())
p = [int(_) for _ in 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
    elif p[i+1] < p[i] and p[i] <= p[i-1]:
        ans = ans + 1
        # print(p[i-1], p[i],p[i+1])

print(ans)

Submission status: Accepted

Solution code:
# using main() makes code faster from the point of view of "access to variables in global name-space"
# def main():

""" convenient functions
# for i, a in enumerate(iterable)
# q, mod = divmod(a, b)
# divmod(x, y) returns the tuple (x//y, x%y)
# manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
"""

import sys
sys.setrecursionlimit(10**7)
from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
# accumulate() returns iterator! to get list: list(accumulate())
from math import factorial, ceil, floor
def factorize(n):
    """return the factors of the Arg and count of each factor
    
    Args:
        n (long): number to be resolved into factors
    
    Returns:
        list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
    """
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct
def combinations_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    # TODO: How should I do when n - r is negative?
    if n < 0 or r < 0:
        raise Exception('combinations_count(n, r) not defined when n or r is negative')
    if n - r < r: r = n - r
    if r < 0: return 0
    if r == 0: return 1
    if r == 1: return n
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result
def combinations_with_replacement_count(n, r):
    """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
    
    Args:
        n (long): number
        r (long): number
    
    Raises:
        Exception: not defined when n or r is negative
    
    Returns:
        long: number
    """
    if n < 0 or r < 0:
        raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
    elif n == 0:
        return 1
    else:
        return combinations_count(n + r - 1, r)
from bisect import bisect_left, bisect_right
from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
from operator import itemgetter
# ex1: List.sort(key=itemgetter(1))
# ex2: sorted(tuples, key=itemgetter(1,2))
from functools import reduce
def chmin(x, y):
    """change minimum
    if x > y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current minimum value
        y (long): potential minimum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x > y:
        x = y
        return (x, True)
    else:
        return (x, False)
def chmax(x, y):
    """change maximum
    if x < y, x = y and return (x, True).
    convenient when solving problems of dp[i]
    
    Args:
        x (long): current maximum value
        y (long): potential maximum value
    
    Returns:
        (x, bool): (x, True) when updated, else (x, False)
    """
    if x < y:
        x = y
        return (x, True)
    else:
        return (x, False)

from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers):
    return reduce(lcm, numbers, 1)

# first create factorial_list
# fac_list = mod_factorial_list(n)
INF = 10 ** 18
MOD = 10 ** 9 + 7
modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
def modinv(a, p = MOD):
    # evaluate reciprocal using Fermat's little theorem:
    # a**(p-1) is identical to 1 (mod p) when a and p is coprime
    return modpow(a, p-2, p)
def modinv_list(n, p = MOD):
    if n <= 1:
        return [0,1][:n+1]
    else:
        inv_t = [0,1]
        for i in range(2, n+1):
            inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
        return inv_t
def modfactorial_list(n, p = MOD):
    if n == 0:
        return [1]
    else:
        l = [0] * (n+1)
        tmp = 1
        for i in range(1, n+1):
            tmp = tmp * i % p
            l[i] = tmp
        return l
def modcomb(n, k, fac_list = [], p = MOD):
    # fac_list = modfactorial_list(100)
    # print(modcomb(100, 5, modfactorial_list(100)))
    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    if len(fac_list) <= n:
        a = factorial(n) % p
        b = factorial(k) % p
        c = factorial(n-k) % p
    else:
        a = fac_list[n]
        b = fac_list[k]
        c = fac_list[n-k]
    return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
def modadd(a, b, p = MOD):
    return (a + b) % MOD
def modsub(a, b, p = MOD):
    return (a - b) % p
def modmul(a, b, p = MOD):
    return ((a % p) * (b % p)) % p
def moddiv(a, b, p = MOD):
    return modmul(a, modpow(b, p-2, p))

""" initialize variables and set inputs
# initialize variables
    # to initialize list, use [0] * n
    # to initialize two dimentional array, use [[0] * N for _ in range(N)]
# set inputs
    # open(0).read() is a convenient method:
    # ex) n, m, *x = map(int, open(0).read().split())
    #     min(x[::2]) - max(x[1::2])
    # ex2) *x, = map(int, open(0).read().split())
    #     don't forget to add comma after *x if only one variable is used
# preprocessing
    # transpose = [x for x in zip(*data)]
    # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    # flat = [flatten for inner in data for flatten in inner]
    # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
# calculate and output
    # output pattern
    # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
"""

# functions used
r = lambda: sys.stdin.readline().strip()
r_int = lambda: int(r())
R = lambda: list(map(int, r().split()))
Rfloat = lambda: list(map(float, r().split()))
Rtuple = lambda: tuple(map(int, r().split()))
Rmap = lambda: map(int, r().split())

""" how to treat input
# single int: int(r())
# single string: r()
# single float: float(r())
# line int: R()
# line string: r().split()
# line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
# lines int: [R() for _ in range(n)]
"""

# for test

# ----------------------------------
# main
N = int(r())
P = R()
count = 0
for i in range(1,N-1):
    p1 = P[i-1]
    p2 = P[i]
    p3 = P[i+1]
    if p1 < p2 < p3 or p3 < p2 < p1:
        count += 1
print(count)

# end of main
# ----------------------------------

"""memo: how to use defaultdict of list
# initialize
Dic = defaultdict(list)
# append / extend
Dic[x].append(y)
# for
for k, v in Dic.items():
"""

# if __name__ == '__main__':
#     main()

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
count = 0
for i in range(1,n-1):
  sor = sorted(p[i-1:i+2])
  if p[i] == sor[1]:
    count += 1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[int(s) for s in input().split()]
cnt=0
for i in range(1,len(l)-1):
  re1=l[i-1]
  re2=l[i]
  re3=l[i+1]
  if( (re2>re1 and re2<re3) or (re2>re3 and re2<re1)):
    cnt+=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l = input().split()
N = int(l[0])
P = list(map(int, input().split()))
# print(N, P)

ans = 0
for i in range(len(P) - 2):
    Q = sorted(P[i:i+3])
    if Q[1] == P[i + 1]:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc132/tasks/abc132_b

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

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

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, p: "List[int]"):
    res = 0
    for i in range(len(p)-2):
        if p[i+1] == list(sorted(p[i:i+3]))[1]:
            res += 1
    print(res)


    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    P = LIST()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = 0
b = list(map(int,input().split()))
for x in range(1,n-1):
    if min(b[x-1], b[x], b[x+1]) == b[x]:
        a += 0
        continue
    elif max(b[x-1], b[x], b[x+1]) == b[x]:
        a += 0
        continue
    else:
        a += 1
        continue
print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    n,*a=map(int,open(0).read().split())
    ans=0
    for i in range(3,n+1):
        if a[i-3:i][1]==sorted(a[i-3:i])[1]:
            ans+=1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
answer = 0
for i in range(1, n - 1):
    tmp = [p[i-1], p[i], p[i+1]]
    if len(set(tmp)) != 3:
        break
    if sorted(tmp)[1] == p[i]:
        answer += 1
print(answer)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
p=list(map(int,input().split()))
count = 0
for i in range(n-2):
    if sorted(p[i:i+3])[1] == p[i+1]:
        count += 1
print(count)

Submission status: Accepted

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

ans = 0
for i in range(1, N - 1):
    p1, p2, p3 = P[i - 1], P[i], P[i + 1]
    if (p1 < p2 < p3) or (p3 < p2 < p1):
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
count = 0
for i in range(len(s)-2):
    if s[i] <s[i+1]<s[i+2] or s[i] >s[i+1]>s[i+2]:
        count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
P = [int(_) for _ in input().split()]

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

print(ans)

Submission status: Accepted

Solution code:
# -*- encoding: UTF-8 -*-

n = int(input())

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

def isSecond(tgt, p1, p2) :
    if p1 > p2 :
        tmp = p1
        p1 = p2
        p2 = tmp
    if p2 > tgt and tgt > p1 :
        return True
    return False

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int,input().split()))
cnt = 0

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

Submission status: Accepted

Solution code:
#ABC132B
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 = count + 1
    else:
        pass

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, p):
    ans = 0
    for i in range(1, n-1):
        if p[i] == sorted(p[i-1:i+2])[1]:
            ans += 1
    return ans

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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=ans+1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in 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
    if (p[i - 1] > p[i] and p[i] > p[i + 1]):
        cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = np.array(list(map(int,input().split())))
print(np.count_nonzero((p[1:n-1]-p[:n-2])*(p[2:n]-p[1:n-1])>0))

Submission status: Accepted

Solution code:
N = int(input())
*P, = map(int, input().split())
ans = 0
for i in range(1, N-1):
    if P[i] == sorted(P[i-1: i+2])[1]: ans += 1
print(ans)

Submission status: Accepted

Solution code:
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 += 1
    elif p[i] > p[i+1] and p[i+1] > p[i+2]: 
        count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
num_array = [int(i) for i in input().split()]

three_nums = [0,0,0]

ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

count = 0

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
print(len([i 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]]))

Submission status: Accepted

Solution code:
n = int(input())
numbers = input()

numbers = [int(x)  for x in numbers.split()]

smallest = set()
for i in range(1,len(numbers) - 1):
    #print(numbers[i])
    if (numbers[i] < numbers[i -1] and numbers[i] > numbers[i + 1] ) or (numbers[i] < numbers[i +1] and numbers[i] > numbers[i - 1] ):
        if not(numbers[i] in smallest):
            smallest.add(numbers[i])
    
        
print(len(smallest))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
count = 0
i = 0
while(i < n-2):
  temp = lis[i:i+3]
  if temp[1] == sorted(temp)[1]:
    count += 1
  i += 1
  
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input_n = int(input())
input_items = list(map(int, input().split(' ')))

cnt = 0
for i in range(input_n - 2):
    targets = [input_items[i + j] for j in range(3)]
    if input_items[i + 1] != min(targets) and input_items[i+1] != max(targets):
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
#print(n)
#print(p)
a=0
i=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]):
        a=1+a
print(a)
    #sumir=vie(i)+sumir
#print("")
#print(sumir)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

size=input()
P=input()

p=P.split(" ")
p=np.array(p,dtype=np.int32)

counter=0

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

print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
number = int(input())
numbers = tuple(map(lambda x:int(x), input().split(" ")))

counts = sum(map(lambda i: 1 if (numbers[i]-numbers[i+1])*(numbers[i+1]-numbers[i+2])>0 else 0 ,range(number-2)))

print(counts)

Submission status: Accepted

Solution code:
def calc(n, p):
    count = 0
    for i in range(1, n-1):
        if p[i-1] < p[i] and p[i] < p[i+1]:
            count += 1
            continue
        if p[i-1] > p[i] and p[i] > p[i+1]:
            count += 1
            continue
    return count
print(calc(int(input()), [int(a) for a in input().split()]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(_) for _ in input().split()]

result = 0

for i in range(0, N - 2):
    if P[i] < P[i+1] < P[i+2] or P[i] > P[i+1] > P[i+2]:
        result += 1

print(result)

Submission status: Accepted

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

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

print('{}'.format(ans))

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i) for i in input().split()]
print(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]]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, p: "List[int]"):
    count = 0
    for i in range(1, n -1):
        if sorted([p[i-1], p[i], p[i+1]])[1] == p[i]:
            count += 1
    print(count)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(cnt)

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print('{}'.format(ans))

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))
c = 0
for i in range(n-2):
  if (s[i+1] - s[i]) * (s[i+1] - s[i+2]) < 0:
    c += 1
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i)for i in input().split()]
cnt = 0
for n in range(1,N-1):
    if (P[n-1] < P[n] < P[n+1] or P[n-1] > P[n] > P[n+1]):
        cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))  

count=0

for i in range(1,N-1):
    re_list=list([H[i-1],H[i],H[i+1]])
    re_list.sort()
    if re_list[1]==H[i]:
        count+=1


print(count)

Submission status: Accepted

Solution code:
_,p=open(0);*p,=map(int,p.split());print(sum(sorted(a)[1]==a[1]for a in zip(p,p[1:],p[2:])))

Submission status: Accepted

Solution code:
a=int(input())
li = list(map(int,input().split()))	
count = 0
for i in range(1,a-1):
  if li[i-1]<li[i] and li[i]<li[i+1]:
    count += 1
  elif li[i-1]>li[i] and li[i]>li[i+1]:
    count += 1
  else:
    pass
print(count)

Submission status: Accepted

Solution code:
_,p=open(0);*p,=map(int,p.split());print(sum(a<b<c or c<b<a for a,b,c in zip(p,p[1:],p[2:])))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
from sys import stdin
import numpy as np

def main():
    #pass
    n = int(stdin.readline().rstrip())
    p = np.array([int(x) for x in stdin.readline().rstrip().split()])
    #print(n,p_list,type(p_list))
    count=0
    for i in range(1,n-1):
        #print(p[i])
        #p_list[i+1] p_2
        if p[i-1]<p[i]<=p[i+1]:
            count+=1
        if p[i-1]>=p[i]>p[i+1]:
            count+=1
    print(count)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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]):
        count += 1
    elif (p[i] > p[i-1]) and (p[i] < p[i+1]):
        count += 1

print(count)

Submission status: Accepted

Solution code:
li = lambda: list(map(int, input().split()))

n = int(input())
p = li()
count = 0
for i in range(n-2):
    i_n = p[i]
    ni = p[i+1]
    n_i = p[i+2]
    if (i_n < ni < n_i) or (i_n > ni > n_i):
        count += 1
print(count)

Submission status: Accepted

Solution code:
long = int(input())
p = input()
plng = []
pstr = p.split(" ")
n = 0

for i in range(long):
    plng.append(int(pstr[i]))



for i in range(long-2):
    three = [plng[i],plng[i+1],plng[i+2]]
    three.sort()

    if three[1] == plng[i + 1]:
     n = n + 1

print(n)