CF BUDDY
← Problems·

1975C · Chamo and Mocha's Array

1200 · binary search, brute force, greedy

Problem: Mocha likes arrays, so before her departure, Chamo gave her an array aa consisting of nn positive integers as a gift.

Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:

  1. Choose indices ll and rr (1l<rn1 \leq l < r \leq n)
  2. Let xx be the median^\dagger of the subarray [al,al+1,,ar][a_l, a_{l+1},\ldots, a_r]
  3. Set all values al,al+1,,ara_l, a_{l+1},\ldots, a_r to xx

Suppose a=[1,2,3,4,5]a=[1,2,3,4,5] initially:

  • If Mocha chooses (l,r)=(3,4)(l,r)=(3,4) in the first operation, then x=3x=3, the array will be changed into a=[1,2,3,3,5]a=[1,2,3,3,5].
  • If Mocha chooses (l,r)=(1,3)(l,r)=(1,3) in the first operation, then x=2x=2, the array will be changed into a=[2,2,2,4,5]a=[2,2,2,4,5].

Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.

^\dagger The median in an array bb of length mm is an element that occupies position number m+12\lfloor \frac{m+1}{2} \rfloor after we sort the elements in non-decreasing order. For example, the median of [3,1,4,1,5][3,1,4,1,5] is 33 and the median of [5,25,20,24][5,25,20,24] is 2020.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001\leq t\leq 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (2n1052\leq n\leq 10^5) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ai1091\leq a_i \leq 10^9) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, output the maximum value of the number.

Note: In the first test case, a=[1,2]a=[1,2]. Mocha can only choose the interval (l,r)=(1,2)(l,r)=(1,2). The array will be changed to a=[1,1]a=[1,1]. Therefore, the answer is 11.

In the second test case, Mocha can perform the following operations:

  • Choose the interval (l,r)=(4,5)(l,r)=(4,5), then a=[1,2,3,4,4]a=[1,2,3,4,4].
  • Choose the interval (l,r)=(3,5)(l,r)=(3,5), then a=[1,2,4,4,4]a=[1,2,4,4,4].
  • Choose the interval (l,r)=(1,5)(l,r)=(1,5), then a=[4,4,4,4,4]a=[4,4,4,4,4].

The array contains only the same number, which is 44. It can be proven that the maximum value of the final number cannot be greater than 44.

Sample Cases

Case 1

Input

2
2
1 2
5
1 2 3 4 5

Output

1
4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback