Optimal trip P46672


Statement
 

pdf   zip

You are planning a trip on a straight road where locations are defined by its distance to some reference point. The trip will start at x1x_1, it will pass through points x2x_2, …, xn1x_{n-1} in this order, and it will end at xnx_n, with x1<x2<<xn1<xnx_1 < x_2 < \dots < x_{n-1} < x_n. You will make exactly two stops, say at points xix_i and xjx_j, with 1<i<j<n1 < i < j < n. You want to make the three distances xix1x_i - x_1, xjxix_j - x_i and xnxjx_n - x_j as similar as possible. More precisely, your goal is to minimize the difference between the maximum and the minimum of those three distances.

For instance, supose a travel defined with 4 10 23 32 42 50. Here, the optimal choice is to stop at 23 and 32, which gives the distances 234=1923 - 4 = 19, 3223=932 - 23 = 9 and 5032=1850 - 32 = 18. In this case, the difference is 199=1019 - 9 = 10. It is easy to see that we cannot make the difference smaller by choosing two other stopping points.

Input

Input consists of several cases. Each case starts with nn, followed by x1x_1, …, xnx_n. You can assume 4n1054 \le n \le 10^5, and 0x1<x2<<xn1<xn1090 \le x_1 < x_2 < \dots < x_{n-1} < x_n \le 10^9.

Output

For every case, print the minimum difference if we choose the optimal stops.

Public test cases
  • Input

    6  4 10 23 32 42 50
    4  0 200000000 700000000 1000000000
    5  100000 240000 300000 500000 700000
    

    Output

    10
    300000000
    0
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++