Given an array of integers, find "the next to the middle".
The next to the middle is the smallest integer greater than the smallest among mean, median and mode of the given numbers, that is neither the mean, median or mode and is also contained in the array.
For example, in the following array
[ 7, 5, 2, 8, 0, 2, 9, 3, 5, 1, 2 ]
Mean: 4
Median: 3
Mode: 2
The next to the middle is 5
, because:
- It's greater than 2 (the smallest of the three)
- It is not any of the mean, median, mode
- It's present in the input array
- It's the smallest number matching the above requirements
Another example, given this array
[ 2, 5, 1, 11, -1, 2, 13, 5, 1, 0, 5 ]
Mean: 4
Median: 2
Mode: 5
The next to the middle is 11
.
Input
An array containing a sequence of integers.
You don't need to handle integers larger than those that your language's data type can handle
The mean could be a floating point number and that's just fine.
If the number of elements in the input array is even, you need to handle 2 medians, rather than doing the average of the two values.
If more than one integer occurs with the highest frequency, you need to handle multiple values for the mode.
Output
The next to the middle.
If such number doesn't exist you can output either 3.14
, an empty string or any other value that cannot be mistaken for an element of the array (be consistent with that value throughout your program).
Standard rules apply for your answer, with standard I/O conventions, while default Loopholes are forbidden.
It would be nice if you could provide an easy way to try your program and possibly an explanation of how it works.
This is code-golf, the shortest wins.
Test cases
[ 7, 5, 2, 8, 0, 2, 9, 3, 5, 1, 2 ]
5
[ 2, 5, 1, 11, -1, 2, 13, 5, 1, 0, 5 ]
11
[ 528, -314, 2, 999, -666, 0, 0, 78 ]
78
[ 528, -314, 2, 999, -666, 0, 0, 79 ]
79
[ 528, -314, 2, 999, -666, 5, -5, 42 ]
NaN
[ -845, 2021, 269, 5, -1707, 269, 22 ]
5
[ -843, 2021, 269, 5, -1707, 269, 22 ]
2021
[-54,-22,-933,544,813,4135,54,-194,544,-554,333,566,566,-522,-45,-45]
333
[95444,-22668,834967,51713,321564,-8365542,-962485,-253387,-761794,-3141592,-788112,533214,51713,885244,522814,-41158,-88659176,654211,74155,-8552445,-22222]
-3141592
[ 1, 2, 3, 9, 8, 7, 9, 8, 5, 4, 6, 0, 6, 7 ]
NaN
[ ] // empty array
NaN
You don't need to handle an empty array.
Answers to comments
Is any normal floating point accuracy acceptable?
Yes. Since you will compare the mean with integers, considering 9,87654321 as mean is the same thing of considering 9.8 as mean."If such number doesn't exist", can we error?
Since there's a reusability rule for functions, if you are writing a function, a program needs to be able to call that function multiple times. If you are writing a program, then you can exit on error, but you have to be consistent with this output: you have to exit on error every time that there is no next to the middle.