Connection Status:
Competition Arena > MedianOfNumbers
SRM 308 · 2006-06-24 · by Andrew_Lazarev · Simple Math
Class Name: MedianOfNumbers
Return Type: int
Method Name: findMedian
Arg Types: (vector<int>)
Problem Statement

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 int[] numbers. Return the median of numbers or -1 if numbers has no median.

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.
Examples
0)
{1, 4, 2, 5, 7}
Returns: 4

The example from the statement.

1)
{1, 5, 8, 3}
Returns: -1
2)
{7}
Returns: 7

There are zero elements that are greater than 7 and zero elements that are smaller than 7.

3)
{7, 12}
Returns: -1
4)
{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.

Coding Area

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

Submitting as anonymous