Up and Down
InterviewTime limit3sMemory limit1024 MB
Find the longest subsequence that strictly increases up to one peak element and then strictly decreases, with at least two elements on each side.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Binary search
- Solved
- No attempts yet
Problem
Given a sequence of integers with length , find the maximum length of a subsequence that strictly increases and then strictly decreases. The last position of the strictly increasing part must coincide with the first position of the strictly decreasing part. A subsequence is obtained by removing some or no elements from the sequence without changing the order of the remaining elements.
For this problem, the strictly increasing part and the strictly decreasing part must each have length at least 2. An empty subsequence or a subsequence of length 1 does not strictly increase or strictly decrease. See the examples below.
- 7 neither strictly increases nor strictly decreases.
- 1 1 1 neither strictly increases nor strictly decreases.
- 1 1 2 neither strictly increases nor strictly decreases.
- 1 2 7 strictly increases. It does not strictly decrease.
- 3 2 1 strictly decreases. It does not strictly increase.
- 8 9 3 strictly increases and then strictly decreases. This is the type of subsequence your solution needs to find.
Input
The first line contains a positive integer (), the number of test cases.
Each test case consists of two lines. The first line contains a positive integer (), the number of integers in the list. The second line contains integers , each in the range of a signed 32-bit integer ().
The total number of integers across all test cases does not exceed .
Output
For each test case, output a single non-negative integer: the maximum length among all subsequences that strictly increase and then strictly decrease. For example, in Sample Input 3, the subsequence 1 2 4 5 2 1 is the longest up-and-down subsequence, so the answer is .