THE NEXT CTO

Problem Statement

CTO stands for Chief Technology Officer and the person holding this position is responsible for all the technical decisions taken by the organization. Sounds amazing, right? So assume that you are the CTO of VIIT Tech Solutions. One day you found a bug in the production environment and wanted to resolve it as soon as possible. The task is to find the total number of valley points present in a sequence of N numbers. A valley point is formed at index i if the value at i is lesser than both the neighboring values i-1 and i+1. There exist no valley points at the 1st index and Nth index. So find the total number of valley points.

Input Format

  • The first line contains T, the number of test cases.
  • Each test case contains N, the size of the array.
  • Followed by N numbers.

Output Format

  • Print the total number of valley points.

Constraints

  • 1 <= T <= 10
  • 1 <= N <= 105
  • -109 <= A[i] <= 109
Sample Input
Sample Output

Solution

file_type_python the_next_cto.py
for _ in range(int(input())):
    n = int(input())
    l = list(map(int, input().split()))
    cnt = 0
    if n < 3:
        print('0')
        break
    for i in range(1, n - 1):
        if l[i] < ;[i - 1] and l[i] < l[i + 1]:
            cnt += 1
    print(cnt)

Comments

Load Comments