MedianOfNumbers
SRM 308 · 2006-06-24 · by Andrew_Lazarev
Problem Statement
In a set of distinct numbers, the median is an element M such that the number of elements greater than M is equal to the number of elements smaller than M. For example, in a set {1, 4, 2, 5, 7} the median is 4 because two elements (5 and 7) are greater than 4 and 2 elements (1 and 2) smaller than 4. The set {1, 5, 8, 3} has no median because no element from it satisfies the definition above.
You are given a
Constraints
- numbers will contain between 1 and 50 elements, inclusive.
- Each element of numbers will be between 1 and 100, inclusive.
- All elements of numbers will be distinct.
{1, 4, 2, 5, 7}
Returns: 4
The example from the statement.
{1, 5, 8, 3}
Returns: -1
{7}
Returns: 7
There are zero elements that are greater than 7 and zero elements that are smaller than 7.
{7, 12}
Returns: -1
{66, 53, 47, 86, 18, 21, 97, 92, 15}
Returns: 53
Submissions are judged against all 91 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MedianOfNumbers with a public method int findMedian(vector<int> numbers) · 91 test cases · 2 s / 256 MB per case