Question: Let $f:[0,1]\to(0,\infty)$ be a function satisfying $$\int_0^1f(x)dx=1.$$ Show that the integral $$\int_0^1(x-a)^2f(x)dx\text{ is minimized when } a=\int_0^1xf(x)dx.$$ Hence or otherwise show that $$\int_0^1|x-\mu|f(x)dx\le \frac{1}{2}, \text { where } \mu=\int_0^1xf(x)dx.$$
My approach: Let $g:\mathbb{R}\to\mathbb{R}$ be such that $$g(a)=\int_0^1(x-a)^2f(x)dx, \forall a\in\mathbb{R}.$$
Thus $$g(a)=\int_0^1 x^2f(x)dx-2a\int_0^1xf(x)dx+a^2\int_0^1f(x)dx\\=\int_0^1 x^2f(x)dx-2a\int_0^1xf(x)dx+a^2, \forall a\in\mathbb{R}.$$
Observe that $g$ is differentiable $\forall a\in\mathbb{R}$.
Now $$g'(a)=-2\int_0^1xf(x)dx+2a, \forall a\in\mathbb{R}.$$
Thus $$g'(a)=0\iff -2\int_0^1xf(x)dx+2a=0\iff a=\int_0^1xf(x)dx.$$
Again observe that $g'$ is differentiable $\forall a\in\mathbb{R}$ and $$g''(a)=2, \forall a\in\mathbb{R}.$$
This implies that $$g''\left(\int_0^1xf(x)dx\right)=2>0.$$
Hence by the double derivative test we can conclude that $g$ has a local minimum at $a=\int_0^1xf(x)dx$.
Now observe that $$\lim_{a\to+\infty}g(a)=+\infty\text{ and }\lim_{a\to-\infty}g(a)=+\infty.$$
Thus we can conclude that $g$ attain it's global minimum at $$a=\int_0^1 xf(x)dx.$$
Hence, we are done with the first part of the question.
For the second part I was trying to solve using the Cauchy-Schwarz inequality for integrals, but haven't found anything meaningful yet. Someone please help me in proceeding.
Let $A = \int_0^\mu |x-\mu| f(x) dx$ and $B = \int_\mu^1 |x-\mu| f(x)dx$. The integral at hand equals to
$$I \stackrel{def}{=} \int_0^1 |x-\mu|f(x) dx = A + B$$
Since $$B - A = \int_\mu^1 (x-\mu) f(x) dx - \int_0^\mu (\mu - x) f(x)dx = \int_0^1 (x - \mu) f(x) dx = 0$$ we have $A = B \implies I = 2A = 2B$.
Notice $$\left[\int_0^\mu f(x) dx - (1-\mu)\right] + \left[\int_\mu^1 f(x) dx - \mu \right] = \int_0^1 f(x) dx - 1 = 0$$ At least one of the square brackets on LHS is negative.
Let's say $\int_0^\mu f(x)dx \le 1 - \mu$, we will have
$$A = \int_0^\mu |x-\mu| f(x) dx \le \mu \int_0^\mu f(x) dx \le \mu(1-\mu)$$
Otherwise, $\int_\mu^1 f(x) dx < \mu$ and $$B = \int_\mu^1 |x-\mu| f(x) dx \le (1-\mu)\int_\mu^1 f(x) dx \le (1-\mu)\mu$$
In both cases, we find $I \le 2\mu(1-\mu)$. It is easy to see $\mu \in [0,1]$, this leads to $$\int_0^1 |x-\mu|f(x) dx \le \sup_{\mu \in [0,1]} 2\mu(1-\mu) = \frac12$$
As an alternate approach, we can use the fact $f(x)$ is non-negative and integrate to $1$. This allow us to treat $f(x)$ as a probability density for a random variable $X$ takings values in $[0,1]$ with mean $\mu$. Let $\sigma^2$ be the variance of $X$.
By a variant of Cauchy Schwarz inequality, we have
$$I^2 = \verb/E/\bigg[|X-\mu|\cdot 1\bigg]^2 \le \verb/E/\left[(X - \mu)^2\right]\verb/E/\bigg[1^2\bigg] = \sigma^2$$ By Popoviciu's inequality on variances, we get
$$\sigma^2 \le \frac14(1 - 0)^2 = \frac14 \quad\implies\quad \int_0^1 |x-\mu|f(x) dx \le \sigma \le \frac12 $$