TopCoder problem "MedianOfNumbers" used in SRM 308 (Division II Level One)



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.

 

Definition

    
Class:MedianOfNumbers
Method:findMedian
Parameters:int[]
Returns:int
Method signature:int findMedian(int[] numbers)
(be sure your method is public)
    
 

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
5)
    
{32, 54, 27, 4, 69, 96, 73, 1, 100, 15, 21}
Returns: 32

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6528

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9988&pm=6528

Writer:

Andrew_Lazarev

Testers:

PabloGilberto , brett1479 , vorthys , Olexiy

Problem categories:

Simple Math