#K69442. Rearranging Array with Bounded Differences

    ID: 33087 Type: Default 1000ms 256MiB

Rearranging Array with Bounded Differences

Rearranging Array with Bounded Differences

Given an array A consisting of N integers and an integer K, determine if it is possible to rearrange the array such that the absolute difference between any two consecutive elements is at most K. In other words, you need to check whether there exists a permutation (P) of the array A such that for every consecutive pair (P_i) and (P_{i+1}) (where (1 \le i < N)), the condition (|P_{i+1} - P_i| \le K) holds.

One straightforward approach is to sort the array and verify if every pair of adjacent elements satisfies the condition. If any pair fails the condition, the answer is "NO"; otherwise, it is "YES".

inputFormat

Input is read from stdin. The first line contains two space-separated integers (N) and (K). The second line contains (N) space-separated integers representing the elements of the array A.

outputFormat

Output a single line to stdout containing either "YES" if the array can be rearranged so that the difference between any two consecutive elements is at most (K), or "NO" otherwise.## sample

5 2
1 3 5 8 10
NO