Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given any set of real scalar values $V=\{v_i | 1 \leq i \leq n\}$ and a distinct value $v_p$ define c:-

$$ c= \sum_{i=1}^n |v_i-v_p| $$

What is the easiest way to determine $v_p$ such that $c$ is minimised?

One approach would be an iterative binary search for $v_p$ between $\min(V)$ and $\max(V)$ - can this be improved upon?

share|cite|improve this question
up vote 3 down vote accepted

The median of the $v_i$ minimizes this function, so you should sort the $v_i$ and pick the middle value, which takes $O(n\log n)$ time.

If $n$ is even, pick any value between the middle two elements (it is easy to see that changing $v_p \rightarrow v_p + \delta v_p$ doesn't change $c$ unless $v_p$ crosses one of the $v_i$.)


A binary search would find an optimal value in $O(\log n)$ function evaluations, each of which executes in $O(n)$ time, for a total running time of $O(n\log n)$.

If you follow my advice and sort the $v_i$ to find the median, it will take $O(n\log n)$ comparisons on average. However, you can be smarter: using a selection algorithm it is possible to compute the sample median in $O(n)$ time, and this is a lower bound on the complexity.

share|cite|improve this answer

Plot $n$, say: $9$, arbitrary points $v_i$ on a horizontal $v$-axis and mark in red a trial point $x$ in the midst between these. Now check what happens to the quantity $c(x):=\sum_{i=1}^n |v_i-x|$ when you move $x$ slightly to the left or to the right. In the end you will see where $x$ must lie to make $c(x)$ minimal. (The solution may be non-unique, as, e.g., in the case $n=2$.)

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.