Avoid Maxima-Minima

3.4

7 votes
String Searching, Segment tree, Data Structures, Segment Trees, String Algorithms, Advanced Data Structures, Algorithms
Problem

You are given an array A consisting of N integers.

An index of the array i (1<i<N) is called :

  • local maximum if the element on the ith index is strictly greater than the two adjacent elements (that is, Ai>Ai1,Ai>Ai+1 )
  • local minimum if the element on the ith index is strictly less than the two adjacent elements (that is,Ai<Ai1,Ai<Ai+1).

You want to obtain an array such that it does not contain any local maximums and local minimums. To do that, you can do the following operation any number of times:

  • Choose an index i (1i≤∣A) and delete the element A[i] from the array. Here A denotes the current length of array A. After each operation, the length of the array decreases by one.

Find the maximum length of the final array A which does not contain any local maximums and local minimums.

Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains denoting the size of array A.
    • The second line contains N space-separated integers A[1], A[2], ....., A[N] - denoting the elements of A.

Output format
For each test case, print the maximum length of the final array A which does not contain any local maximums and local minimums.

Constraints

1T1051N21051AiNSumofNoveralltestcasesdoesnotexceed2105.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

 The first test case

  • The given array does not contain any local maximums and local minimums. Hence no operation is required.

The second test case

  • Choose index i = 3 and delete A[3], making A = [5, 4, 4, 3]. Now array A does not contain any local maximums and local minimums.
Editor Image

?