This page is still under construction.

Parts of this page are still being built. What you see may change.

Easter Gift

Interview

Time limit1sMemory limit512 MB

Summary
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 NN elements (a1,a2,…,aNa_1, a_2, \ldots, a_N) for Easter, and is eager to sort it (so that a1≤a2≤…≤aNa_1 \le a_2 \le \ldots \le a_N). 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 KK. Note that the elements can be anywhere; as long as their absolute difference is less than or equal to KK, 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 KK required to be able to sort the array?

Input

The first line contains an integer NN, the number of elements in the array (1≤N≤2⋅1051 \le N \le 2 \cdot 10^5).

The next line contains NN integers a1,a2,…,aNa_1, a_2, \ldots, a_N, the array itself (1≤ai≤10181 \le a_i \le 10^{18}).

Output

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

Examples1

  1. Example 1

    Input
    8
    1 4 4 2 7 14 12 10
    
    Expected output
    2