Find a number having minimum sum of distances between a set of numbers

You are looking to minimize $$\sum_{y \in A} |y - x|$$ with respect to $x$ where $A$ is your set.

It can be proved that any median minimizes this problem. In your case, the only median is $5$, so that's the result.


First sort your [multi]set: $\{1, 2, 5, 5, 7, 100\}$. The number you want is $5$. The sum is $4 + 3 + 0 + 0 + 2 + 95 = 104.$

Proof: suppose you have another number $n \neq 5$.

Note that for any number $x$, $|x - a| + |x - b| \le |a - b|$. Hence, it must hold that the sum of its distances to the two $5$s, i.e. $$|n - 5| + |n - 5| \ge |5 - 5| + |5 - 5| = 0.$$ Similarly, $$|n - 2| + |n - 7| \ge |5 - 2| + |5 - 7| = 5,$$ $$|n - 1| + |n - 100| \ge |5 - 1| + |5 - 100| = 99.$$

You can't have the total distance any lower. Q.E.D.

In general, first sort your set, then any number between (including) the middle two numbers will do. For example, for set ${1, 2, 3, 4, 5, 6}$, any $x$ such that $3 \le x \le 4$ does.