#K48322. Taco Rearrangement
Taco Rearrangement
Taco Rearrangement
You are given an array of n integers. Your task is to determine whether it is possible to rearrange the array such that for every element starting from the third one, the following condition holds:
\(a_i < a_{i-1} + a_{i-2}\)
for all \(i \ge 3\). If \(n < 3\), the answer is always "YES".
This condition is analogous to the triangle inequality, and if it is not met for the largest element in the array (after sorting), then no permutation can satisfy the condition. Conversely, if the sorted order meets \(a_n < a_{n-1} + a_{n-2}\), then a valid rearrangement exists.
Find and output "YES" if such rearrangement is possible; otherwise, output "NO".
inputFormat
The first line contains an integer t
representing the number of test cases. Each test case consists of two lines:
- The first line contains an integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers.
outputFormat
For each test case, output a single line with either "YES" if the array can be rearranged to satisfy the condition, or "NO" otherwise.
## sample3
4
4 1 3 2
3
10 20 30
5
1 3 5 7 9
YES
NO
YES
</p>