#C10470. Rearrange Array with Unique Differences
Rearrange Array with Unique Differences
Rearrange Array with Unique Differences
You are given an array of n positive integers. Your task is to rearrange the elements of the array such that the differences between every two consecutive elements are unique. Formally, if the resulting permutation is \(p_1, p_2, \ldots, p_n\), then the set of differences \(\{ |p_2-p_1|, |p_3-p_2|, \ldots, |p_n-p_{n-1}| \}\) should have size \(n-1\). If there are multiple valid rearrangements, you may output any one of them. If no such rearrangement exists, output "NO".
Note: It can be proven that if the input array contains any duplicate elements, no valid rearrangement exists. In that case, you should output "NO".
inputFormat
The first line contains a single integer \(n\) (the number of elements in the array).
The second line contains \(n\) space separated integers representing the array.
outputFormat
If a valid rearrangement exists, output a single line containing the \(n\) rearranged integers separated by a single space. Otherwise, output "NO".
## sample5
4 1 3 7 2
1 2 3 4 7
</p>