#K65807. Rearrange Array with Maximum Adjacent Difference

    ID: 32279 Type: Default 1000ms 256MiB

Rearrange Array with Maximum Adjacent Difference

Rearrange Array with Maximum Adjacent Difference

You are given an array of n integers and an integer K. Your task is to rearrange the elements of the array such that the absolute difference between any two adjacent elements is at most K when the array is in sorted order. If it is possible to rearrange the array in this way, output the rearranged array in ascending order. Otherwise, output an empty array denoted by [].

Note: The intended rearrangement is the sorted order of the numbers. However, even after sorting, if the difference between any two consecutive numbers is greater than K, then it is not possible to meet the requirement.

Input Format: The input is read from stdin and consists of two lines. The first line contains two integers, n (the number of elements) and K. The second line contains n integers representing the array elements.

Output Format: If rearrangement is possible, print the array in ascending order with the elements separated by a single space. If not, print [].

inputFormat

The first line of input contains two integers n and K separated by a space, where n is the number of elements in the array and K is the maximum allowed difference between adjacent elements after rearrangement. The second line contains n integers separated by spaces representing the elements of the array.

outputFormat

If a valid rearrangement exists, output the rearranged array in ascending order with each pair of adjacent integers having a difference of at most K. Print the numbers in one line separated by a space. If no such rearrangement is possible, output [].

## sample
5 3
4 7 1 3 9
1 3 4 7 9