Skip to main content
6 of 23
added 520 characters in body
anotherOne
  • 1.1k
  • 3
  • 4

Next to the middle

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, 0, 2, 12, 5, 1, 0, 5 ]

Mean: 4
Median: 2
Mode: 5

The next to the middle is 11.

This is , the shortest wins.

Edit:
The input array can be of any length and contains any integer sequence.

If the number of elements in the input array is even, you need to handle 2 medians.

If the there are integers that have the same frequency of the mode, you need to handle a multiple values for the mode.

The mean could be a floating point number, it's fine.

If the next to the middle doesn't exist you can output either 3.14, an empty string or any other sentinel value that can't be mistaken for an element of the array.

Meta

  • Could the definition of the next to the middle be better reformulated?
  • Should the input array always have an odd number of integers, or could we allow two values for the median?
  • Should the input always give an integer mean, or should we take the floor() or round() of the mean, or should we instead just accept the floating point mean (after all it doesn't give problems)
  • Is there anything else to clarify?
  • Can you think of some particular test case to include?
  • Which tags should I use?
  • Could you suggest a better name than "next to the middle"?
  • Do you have any other suggestion to improve the challenge?
anotherOne
  • 1.1k
  • 3
  • 4