You are given an array of positive integers. You are told to make all the numbers equal by doing this operation, i.e. to increase/decrease the value of an array element. The cost of the operation will be the amount of increment/decrement (absolute value). Find the minimum cost required to do this task.
Test Example
Array : 2 3 1 5 2
Answer : 5
At first, it appears as though we should change all values to mean value and that should do the trick. But the optimal answer comes when using the median value. I understand that the mean is more sensitive to outliers but still, I cannot really understand why would changing values to the mean value will not give an optimal answer.