TITLE: How to show that $g$ attains maximum at $0$ or $1$
QUESTION [7 upvotes]: Suppose $f:[0,1]\to\mathbb{R}$ is continuous，define 
$$g:[0,1]\to\mathbb{R},\quad g(x):=\int_0^1|f(t)-x|dt$$
Show that $g$ attains maximum at $0$ or $1$.
I don't know how to approach, any hints?

REPLY [2 votes]: Essentially we need to use convexity of g(x). For any $x,t\in(0,1)$, we have 
$$
|f(t)-x|\le x|f(t)-1|+(1-x)|f(t)|
$$
Integrate on both sides form $0$ to $1$ with respect to $t$, we obtain
$$
g(x)\le x\cdot g(1)+(1-x)\cdot g(0)
$$.
Now suppose the contrary, if there exists $x_0\in(0,1)$ such that $g(x_0)>g(0)$ and $g(x_0)>g(1)$, then 
$$
 x_0\cdot g(1)+(1-x_0)\cdot g(0)<g(x_0)\le x_0\cdot g(1)+(1-x_0)\cdot g(0)
$$
which is a contradiction.