Minimizing $|x-1|+|x-2|+|x-3|+...+ |x-2019|+ |x-2020|$. The given expression
$$
|x-1|+|x-2|+|x-3|+...+ |x-2019|+ |x-2020|
$$
determine the greatest interval of real numbers $[a,b]$ on which the given expression has a constant value $k$. What is the value of $k$ ?
I found this question in a facebook group's post. I tried a lot to understand the question. But I didn't understand it at all. A user send answer like that $a=1000$, $b=1001$  and $k=1000000$ . But I don't understand why and how it comes.
 A: Functions of type:
$$f(x)=\sum_{k=1}^n|x-a_k|$$
have minima of two types

*

*with a plateau when $n$ is even or


*with a spike when $n$ is odd.

(See examples on the figure). You are in the first case with $n=2020$.
By symmetry, the plateau occurs "in the middle", i.e., when
$$1010 \le x \le 1011$$
The ordinate of median $1010$ is
$$f(1010)=1009+1008+1007+\cdots+1+0+1+2+\cdot+1010=1010+2\sum_{k=1}^{1009} k=$$
$$1010+2 \times \frac12 1009*1010 = 1010^2$$
(using formula $\sum_{k=1}^{N} k$=\frac12 N*(N+1))$.
Remark: The "big picture" is that in fact, this function is very close to a parabola as can be seen here on a small portion:

A: Hint: Try to gain some intuition about the problem, by plotting the curve for smaller cases ending with even numbers, such as $f(x)=|x-1|+|x-2|$ , or $f(x)=|x-1|+|x-2|+|x-3|+|x-4|$ and such. Treat $|x-a|$ as the distance of a point on the $x$-axis from $x=a$. You will find that minimum of $f(x)=|x-1|+|x-2|+....+|x-2k|$ occurs in the interval $[k,k+1]$ and has value $1+3+5+...+(2k-1)=k^2$.
A: Use that $|a-b|=|b-a|$ and notice that we have by triangle inequality $$|x-y|+|y-z|\geq |x-z|$$
So
\begin{align} |x-1|+|x-2020| &= |1-x|+|x-2020|\geq 2019\\
|x-2|+|x-2019| &= |2-x|+|x-2019|\geq 2017\\
&\vdots \\
|x-1010|+|x-1011| &= |1010-x|+|x-1011|\geq 1\\
\end{align}
Thus $$f(x) \geq 505\cdot 2020 = 1010^2$$
To achieve that value we have to have equality case everywhere.
Say $a<b$, since $|x-a|+|x-b|= |a-b|$ iff $x\in [a,b]$ we see that $f$ achieves that value only if $x\in[1010,1011]$.
A: This answer is more or less the same as the other answers but aims to be more verbose.
Let's rephrase it as such:
$|x - 5|$ is the distance between $x$ and $5$ on the number line. $f(x)$ is the total of distances between $x$ and $1, 2, 3 ... 2020$. Find an interval $[a, b]$ such that every $x ∈ [a, b]$ has the minimum possible value of $f(x)$ which is $k$.

For $x ≤ 1$ as $x$ gets closer to $1$, $f(x)$ becomes smaller.
Keep increasing $x$ until $x = 2$. Notice that $x$ has passed $1$ so $|x - 1|$ is increasing. Furthermore, if we increase $x$ by $d ≤ 1$, $|x - 1|$ also increases by $d$. Using the same logic, we can see that $|x - 2|$ decreases by $d$ as well as $|x - 3|$, $|x - 4|$ ... and so on.
As an example, for $x = 1.3$ and $d = 0.1$:
$|1.3 + 0.1 - 1| = |1.3 - 1| + 0.1$
$|1.3 + 0.1 - 2| = |1.3 - 2| - 0.1$
$|1.3 + 0.1 - 3| = |1.3 - 3| - 0.1$
...
$|1.3 + 0.1 - 2020| = |1.3 - 2020| - 0.1$
So keeping $1 ≤ x ≤ 2$, increasing by $d$ has the net effect of $-2018*d$ on $f(x)$:
$f(x+d) = f(x) - 2018*d$
Keep increasing. After passing $2$, until $x=3$ increasing by $d$ has the net effect of $-2016*d$. We can generalize this as:
Between $[a,a+1]$ increasing by $d$ has the net effect of $(2a - 2020)*d$.
For $f(x)$ to be as small as possible we need the negative net effects on $f(x)$. The last negative effect is in $[1009, 1010]$. Between $[1011, 1012]$ it is positive and we don't want $f(x)$ to increase.
On $[1010, 1011]$, $f(x)$ is minimal and stays the same. We can pick any number and calculate $k$:
For $x = 1010$,
$k = 1009 + 1008 + ... + 1 + 0 + 1 + ... + 1009 + 1010 = 1020100$.
A: Let us use Statistics:
$$f(X)=\sum_{k=1}^{n} |X-X_k|$$ can be raken as $n$ times the mean deviation  of the series $X_k$ about $X$ which is minimum when measured about median. If $n$ is enen there are two medians $M=X_{n/2},X_{n/2+1}$ in this case $f(X)$ attains the equal least value $\forall X \in(X_{n/2},X_{n/2+1})$. When $n$ is odd the median $M$ is $X_{(n+1)/2}$ and $f(X)$ has a local minimum at $X=X_{(n+1)/2}$. In this case $$L=Least[f(X)]=f(1010)=f(1011)\implies L=\sum_{k=1}^{2020} |k-1010|=\sum_{k=1}^{2020} |k-1011|$$
