Under construction

Still being built. Things may change

Up and Down

Interview

Time limit3sMemory limit1024 MB

Summary
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 NN, 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 TT (1≤T≤5⋅1041 \le T \le 5 \cdot 10^4), the number of test cases.

Each test case consists of two lines. The first line contains a positive integer NN (3≤N≤2⋅1053 \le N \le 2 \cdot 10^5), the number of integers in the list. The second line contains NN integers aia_i, each in the range of a signed 32-bit integer (−231≤ai≤231−1-2^{31} \le a_i \le 2^{31}-1).

The total number of integers across all test cases does not exceed 5⋅1055 \cdot 10^5.

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 66.

Examples3

  1. Example 1

    Input
    1
    7
    1 2 3 4 5 6 7 
    
    Expected output
    0
    
  2. Example 2

    Input
    1
    6
    1 2 3 3 2 1 
    
    Expected output
    5
    
  3. Example 3

    Input
    1
    8
    1 11 2 10 4 5 2 1 
    
    Expected output
    6