28
3
Challenge
You are given an array \$a\$ of integers. With a move you can increase or decrease an element of the array by 1. Your task is to equalize the array, that is make all the elements of the array equal by performing some moves. But that's not enough! You also want to make as few moves as possible.
Input
- A non-empty array \$a\$ of integers
- Optionally, the length of \$a\$.
Output
- The minimum number of moves needed to equalize the array \$a\$.
Rules
- Standard rules for valid submissions, I/O, loopholes apply.
- This is code-golf, so shortest solution (in bytes) wins. As usual, don't let ridiculously short solutions in golfy languages discourage you from posting a longer answer in your language of choice.
- This is not a rule, but your answer will be better received if it includes a link to test the solution and an explanation of how it works.
Examples
Input --> Output
[10] --> 0
[-1, 0, 1] --> 2
[4, 7] --> 3
[6, 2, 3, 8] --> 9
[5, 8, 12, 3, 2, 8, 4, 5] --> 19
[1,10,100] --> 99
Random comment:
Median
is a bit too hard for some esoteric languages. – user202729 – 2018-09-29T15:31:56.1801
Looking around a bit, the only submission in esoteric language in the "compute the median" challenge is WW's Brain-Flak one.
– user202729 – 2018-09-29T15:42:37.987