Easter Gift
InterviewTime limit1sMemory limit512 MB
Given an array, find the smallest K such that repeated swaps of pairs whose values differ by at most K can sort it.
- Level
Medium7 of 10
- Topics
- Sorting, Union-find, Binary search, Greedy
- Solved
- No attempts yet
Problem
Wesley got an array of elements () for Easter, and is eager to sort it (so that ). 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?
Input
The first line contains an integer , the number of elements in the array ().
The next line contains integers , the array itself ().
Output
Output the minimum value of required to be able to sort the array. If the elements are already sorted, you should output .