
arrayCenter
Consider an array of integers a
. Let min(a)
be its minimal element, and let avg(a)
be its mean (The arithmetic mean (also known as mean or average) of the list of values x1, x2, ..., xn
, usually denoted by x̅
, is the sum of these values divided by their number, i.e. x̅ = (x1 + x2 + ... + xn) / n
.)
Define the center of the array a
as array b
such that:
b
is formed froma
by erasing some of its elements.- For each
i
,|b[i] - avg(a)| < min(a)
. b
has the maximum number of elements among all the arrays satisfying the above requirements.
Given an array of integers, return its center.
Example
-
For
a = [8, 3, 4, 5, 2, 8]
, the output should bearrayCenter(a) = [4, 5]
.Here
min(a) = 2
,avg(a) = 5
. -
For
a = [1, 3, 2, 1]
, the output should bearrayCenter(a) = [1, 2, 1]
.Here
min(a) = 1
,avg(a) = 1.75
.
Input/Output
-
[execution time limit] 0.5 seconds
-
[input] array.integer a
Unsorted non-empty array of integers.
Guaranteed constraints:
2 ≤ a.length ≤ 1000
,0 ≤ a[i] ≤ 3500
. -
[output] array.integer
Post Comment