Suppose I have the function $f(c) = \sum_{i=1}^{n} (|x_{i}-c|)$, where $x_{i}$ are given positive reals, $1\leq i \leq n$, and $c$ is a real number. How could I find $c$ such that $f$ is minimum?
Edit - additionally, what about the case when $x_{i}$ are positive integers?
This is a little more intuitive framed as a probability question.
Suppose the $x_i, i = 1, 2, ..., n$, are our given numbers, and let $X$ be a random variable which is uniformly distributed on the $x_i$, i.e. $$\Bbb{P}(X = x_i) = 1/n, \text{ for }i = 1, 2, ..., n.$$ Then minimizing $f(c) = \sum_{i=1}^n |x_i - c| \geq 0$ is equivalent to minimizing $$G(c) := \Bbb{E}|c - X| = \frac{f(c)}{n}.$$ Let's take the first derivative: \begin{align*} g(c) &:= G'(c) \\ &= \frac{d}{dc}\Bbb{E}|c - X| \\ &= \Bbb{E} \left[\frac{d}{dc}|c - X| \right] \\ &= \Bbb{E}\operatorname{sgn}(c - X), \ \end{align*} where $\operatorname{sgn}$ is the sign function. We can further calculate this last expectation as \begin{align*} g(c) &= \Bbb{E}\operatorname{sgn}(c - X) \\ &= \Bbb{E}[ \Bbb{1}_{c - X > 0} - \Bbb{1}_{c - X < 0}] \\ &= \Bbb{P}(c - X > 0) - \Bbb{P}(c - X < 0), \ \end{align*} where $1_A$ is the indicator function of event $A$. Clearly, then, we have $g(c)$ is positive, negative, or zero according to whether $c$ is greater than, less than, or equal to the median of $X$. That implies, by First Derivative Test, that $G(c)$ and $f(c)$ are minimized either when: