Find $n$ and $k$ such that maximum element is minimum Given $a_1, a_2, a_3, \ldots, a_m \in \mathbb {Z}$.
How do I find $n \in \mathbb Z, k \in \mathbb N$ such that 
$$\max \{|n - a_1|, |n+k-a_2|, |n+2k-a_3|,...\}$$ is minimum?

The original problem was given $a_1 =2,\ a_2=-3,\ a_3 = -1,\ a_4 =-5$. But I don't know how to solve it, please show me a way or give me a hint.
I'll describe one algorithmic approach to this problem:

Start by imagining we have already determined the best choice of $k$. The problem can then be viewed as trying to find the optimal value of $n$, minimizing $\max\limits_i |n-b_i|$ with $b_i=a_i-(i-1)k$.

It's not too difficult to see that if $b_L$ is the least and $b_G$ the greatest of $b_i$, the optimal choice of $n$ is equal to $\frac{b_L+b_G}{2}$, rounded to the nearest integer. The corresponding value of the maximum is then equal to $\left\lceil\frac{b_G-b_L}{2}\right\rceil$.

This offers one simple (although inefficient) approach to finding the optimal values of $k$ and $n$; just start with $k=1$ and keep increasing it; computing the corresponding optimal value of $n$ in each case, until we find the answer. When can we stop, though?

One useful observation we can make says that with increasing $k$, $L$ never increases and $G$ never decreases and they eventually reach $L=1$ (since $b_1$ does not depend on $k$ at all while all the other $b_i$ keep increasing) and $G=m$ ($b_m$ grows at the fastest pace, so it'll eventually outgrow all the others).

Furthermore, every increase of $k$ by one adds $(G-L)$ to the numerator of $\left\lceil\frac{b_G-b_L}{2}\right\rceil$; as long as neither $G$, nor $L$ changes. Whenever $G$ and/or $L$ changes, the quantity $(G-L)$ actually increases. This means that as $k$ increases, the full expression (maximum of the absolute values) will decrease, reach the optimum and then start to increase again. The optimum point is thus either at $k=1$ (since that's the minimum permitted value) or when $(G-L)$ becomes non-negative. Moreover, the only interesting values of $k$ are those when either $G$ or $L$ changes.

In order to find them, we can look at all pairs $1\leq i&lt;j\leq m$ and calculate $k_{i,j}=\frac{a_j - a_ i}{j-i}$; the value of $k$ when $G$ could possibly change from $i$ to $j$ or $L$ change from $j$ to $i$. Then, we can just try to check the positive integers closest to each of these values, calculate the corresponding $n$ and the corresponding value we're looking for -- and report the smallest one (of course, checking $k=1$ too; due to $k$ being restricted to positive integers). This brings the calculation down to $O(m^2\log m)$ operations (the logarithmic factor is related to sorting the values so that they are processed in the correct order).

This latter part could actually be optimized even further by finding only the "relevant" of the $k_{i,j}$, which there are at most $m$ of and thus reduce the total complexity to linear. The magic word would be "convex hull"
if there's no pattern in your input sequence $a_1$, $a_2$, ... , the formula you gave up there is the best it gets. you can characterize some depending on whether the seq. is (monotonically/-) increasing/decreasing for one.