CF BUDDY
← Problems·

1862C · Flower City Fence

1100 · binary search, data structures, implementation

Problem: Anya lives in the Flower City. By order of the city mayor, she has to build a fence for herself.

The fence consists of nn planks, each with a height of aia_i meters. According to the order, the heights of the planks must not increase. In other words, it is true that aiaja_i \ge a_j for all i<ji < j.

Anya became curious whether her fence is symmetrical with respect to the diagonal. In other words, will she get the same fence if she lays all the planks horizontally in the same order.

For example, for n=5n = 5, a=[5,4,3,2,1]a = [5, 4, 3, 2, 1], the fence is symmetrical. Because if all the planks are laid horizontally, the fence will be [5,4,3,2,1][5, 4, 3, 2, 1], as shown in the diagram.

On the left is the fence [5,4,3,2,1][5, 4, 3, 2, 1], on the right is the same fence laid horizontally

But for n=3n = 3, a=[4,2,1]a = [4, 2, 1], the fence is not symmetrical. Because if all the planks are laid horizontally, the fence will be [3,2,1,1][3, 2, 1, 1], as shown in the diagram.

On the left is the fence [4,2,1][4, 2, 1], on the right is the same fence laid horizontally

Help Anya and determine whether her fence is symmetrical.

Input Format: The first line of the input contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The description of the test cases follows.

The first line of a test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the fence.

The second line of a test case contains nn integers a1a2a3ana_1 \ge a_2 \ge a_3 \ge \dots \ge a_n (1ai1091 \le a_i \le 10^9) — the heights of the planks.

The sum of the values of nn for all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output "YES" if the fence is symmetrical, otherwise output "NO".

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer.

Note: In the first and second test cases of the example, the fence is symmetrical.

In the third test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [3,2,1,1][3, 2, 1, 1].

In the fourth test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [1,1][1, 1].

In the fifth and sixth test cases of the example, the fence is symmetrical.

In the seventh test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be [2,1,1,1,1,1][2, 1, 1, 1, 1, 1].

Sample Cases

Case 1

Input

7
5
5 4 3 2 1
3
3 1 1
3
4 2 1
1
2
5
5 3 3 1 1
5
5 5 5 3 3
2
6 1

Output

YES
YES
NO
NO
YES
YES
NO

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