CF BUDDY
← Problems·

1815A · Ian and Array Sorting

1300 · greedy, math

Problem: To thank Ian, Mary gifted an array aa of length nn to Ian. To make himself look smart, he wants to make the array in non-decreasing order by doing the following finitely many times: he chooses two adjacent elements aia_i and ai+1a_{i+1} (1in11\le i\le n-1), and increases both of them by 11 or decreases both of them by 11. Note that, the elements of the array can become negative.

As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order.

Input Format: The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The description of test cases follows.

The first line of each test case consists of a single integer nn (2n31052\le n\le 3\cdot10^5) — the number of elements in the array.

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

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

Output Format: For each test case, output "YES" if there exists a sequence of operations which make the array non-decreasing, else output "NO".

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

Note: For the first test case, we can increase a2a_2 and a3a_3 both by 11. The array is now [1,4,3][1, 4, 3].

Then we can decrease a1a_1 and a2a_2 both by 11. The array is now [0,3,3][0, 3, 3], which is sorted in non-decreasing order. So the answer is "YES".

For the second test case, no matter how Ian perform the operations, a1a_1 will always be larger than a2a_2. So the answer is "NO" and Ian cannot pretend to be smart.

For the third test case, the array is already in non-decreasing order, so Ian does not need to do anything.

Sample Cases

Case 1

Input

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

Output

YES
NO
YES
NO
YES

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