Absolute Values Sum Minimization
Description
Given a sorted array of integers a
, find an integer x
from a
such that the value of
abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x)
is the smallest possible (here abs
denotes the absolute value).
If there are several possible answers, output the smallest one.
Example
For a = [2, 4, 7]
, the output should be
absoluteValuesSumMinimization(a) = 4
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] array.integer a
A non-empty array of integers, sorted in ascending order.
Guaranteed constraints:
1 ≤ a.length ≤ 200
,
-10^6 \leq a[i] \leq 10^6
.
- [output] integer
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
function absoluteValuesSumMinimization(a) {
return a[Math.floor(a.length/2) - (a.length%2?0:1)];
}