#C11120. Swappable Array Sorting

    ID: 40402 Type: Default 1000ms 256MiB

Swappable Array Sorting

Swappable Array Sorting

Given an array of integers, determine if it is possible to sort the array in non-decreasing order by repeatedly swapping adjacent elements. However, you are only allowed to perform a swap between two adjacent elements if the absolute difference between them is at most ( k ).

In other words, you are given an integer ( n ) representing the number of elements in the array, an integer ( k ) representing the maximum allowed difference for a swap, and an array ( arr ) of ( n ) integers. Determine if the array can be rearranged into non-decreasing order using the allowed adjacent swap operations. A breadth-first search (BFS) approach can be used to simulate the possible swaps.

inputFormat

The first line contains two integers ( n ) and ( k ) separated by a space. The second line contains ( n ) integers representing the elements of the array.

outputFormat

Output a single line containing "YES" if the array can be sorted in non-decreasing order using the allowed swaps, or "NO" otherwise.## sample

5 3
3 1 4 1 5
YES