Avoid Maxima-Minima
Practice
3.4 (7 votes)
String searching
Segment tree
Data structures
Segment trees
String algorithms
Advanced data structures
Algorithms
Problem
16% Success 1162 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array A consisting of N integers.

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

  • local maximum if the element on the \(i^{th}\) index is strictly greater than the two adjacent elements (that is, \(A_i \gt A_{i-1}, A_i \gt A_{i+1}\) )
  • local minimum if the element on the \(i^{th}\) index is strictly less than the two adjacent elements (that is,\( A_i \lt A_{i-1}, A_i \lt A_{i+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 \((1 \le i \le \mid A \mid) \) and delete the element A[i] from the array. Here \(\mid A \mid \) 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

\(1 \leq T \leq 10^5 \\ 1 \leq N \leq 2\cdot10^5\\ \\1\leq A_i \le N \\ Sum\; of \; N\;over\;all\;test\;cases\;does\;not\;exceed\;2\cdot 10^5. \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
4 votes
Tags:
Advanced Data StructuresSegment TreesC++Data Structures
Points:50
2 votes
Tags:
Advanced Data StructuresSegment TreesLinear AlgebraData Structures
Points:50
8 votes
Tags:
Advanced Data StructuresData StructuresSegment Trees