#K91302. Rearrange Array to Satisfy Consecutive Difference Condition
Rearrange Array to Satisfy Consecutive Difference Condition
Rearrange Array to Satisfy Consecutive Difference Condition
Given an array of integers, determine if it is possible to rearrange the array such that for every adjacent pair of numbers in the rearranged array, the absolute difference between them is at most 1. Formally, for an array a of length n, check if there exists a permutation of a such that for every i (1 ≤ i < n),
$$|a_i - a_{i-1}| \le 1$$
If such a rearrangement exists, output "Yes"; otherwise, output "No".
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n integers separated by spaces, representing the elements of the array.
outputFormat
Print a single line to stdout containing either "Yes" if the array can be rearranged to meet the condition, or "No" otherwise.
## sample5
3 1 4 2 5
Yes