-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabsoluteValuesSumMinization.ts
More file actions
29 lines (21 loc) · 1.02 KB
/
absoluteValuesSumMinization.ts
File metadata and controls
29 lines (21 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function absoluteValuesSumMinimization(a: number[]): number {
const isEven = a.length % 2 === 0;
return isEven ? a[a.length / 2 - 1] : a[Math.floor(a.length / 2)];
}
console.log(absoluteValuesSumMinimization([2, 4, 7]));
console.log(absoluteValuesSumMinimization([2, 4, 7, 6]));
console.log(absoluteValuesSumMinimization([2, 4, 7, 6, 6]));
console.log(absoluteValuesSumMinimization([2, 4, 7, 6, 6, 8]));
/* 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.
For a = [2, 4, 7, 6], the output should be
absoluteValuesSumMinimization(a) = 4.
For a = [2, 4, 7, 6, 6], the output should be
absoluteValuesSumMinimization(a) = 7.
For a = [2, 4, 7, 6, 6, 8], the output should be
absoluteValuesSumMinimization(a) = 7. */