I have a list of non repeating positive integers $(a_1,\dots,a_n)$. I need to find integers $x, y$ such that the value of $\sum_{i=1}^n\min\{|a_i-x|,|a_i-y|\}$ is minimal.
For example:
If $a = [2,3,6,7]$ (my list of numbers) then $x$ and $y$ for this list are $3$ and $7$, as:
$|2-3| + |3-3| + |6-7| + |7-7| = 2$ which is the minimum value of the expression.
If $a = [1,3,5]$ then $x$ and $y$ for this list are $1$ and $4$ as:
$|1-1| + |3-4| + |5-4| = 2$ which is the minimum value of the expression.
What is the best approach to find these two integers $x$ and $y$ given that my list of numbers will only contain positive integers?