Equalize the forest (Version 2)

0

0 votes
Medium, D
Problem

Unfortunately, Vessel 1 in the previous problem is broken. Now, you cannot use Vessel 1. Although Vessel 2 works perfectly fine.

Is it still possible to equalize the forest? Print "YES" if it is possible, "NO" otherwise.

NOTE: Please use fast i/o for solving this problem.

Input Format: The first line contains number of test cases t (1t166666). For the next t lines:

The first line contains integer n (1n100000), the number of trees in the forest. Next line contains n space-separated integers i (0i<109), the heights of trees.

It is guaranteed that the sum of n over all test cases doesn’t exceed 106.

Output Format: For each test case print “YES” if it is possible to Equalize the forest, “NO” otherwise.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the third test case, since Tree[2]=tree[3]=1, we can simply apply Vessel 2 on them and equalise the forest as: [ 2, 2, 2, 2]

In the first and second test case, there is no way to equalize the forest

Editor Image

?