## Wesley's Anger Contest 6 Problem 3 - Difference Sorting

View as PDF

Points: 10 (partial)
Time limit: 2.0s
Java 5.0s
Memory limit: 256M

Author:
Problem types

Wesley got an array of elements () for Christmas, and is eager to sort it. Bored, Wesley decided to make it harder on himself by only allowing himself to swap two elements if the absolute difference between them is less than or equal to . Note that the elements can be anywhere; as long as their absolute difference is less than or equal to , Wesley can swap them.

Unfortunately, Wesley quickly realized that it might not be possible to sort the array. He then wonders: what is the minimum value of required to be able to sort the array?

For this problem, Python users are recommended to use PyPy over CPython.

#### Constraints

For this problem, you will be required to pass all the samples in order to receive points. In addition, you must pass all previous subtasks to earn points for a specific subtask.

for all

for all

#### Input Specification

The first line will contain , the number of the elements in the array.

The next line will contain integers , the array.

#### Output Specification

This problem is graded with an identical checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n character and that there are no trailing spaces.

Output the minimum value of required to be able to sort the array. If the elements are already sorted you should output 0.

#### Sample Input

8
1 4 4 2 7 14 12 10

#### Sample Output

2

#### Sample Explanation

If we swap the second and fourth element, we obtain:

1 2 4 4 7 14 12 10

Now we can swap with :

1 2 4 4 7 12 14 10

with :

1 2 4 4 7 10 14 12

And finally, with :

1 2 4 4 7 10 12 14

It can be proven that this is the smallest possible .