TopcoderArchive
SRM24 Jun 2006
SRM 308

MedianOfNumbers

Problem statement, definition, constraints, and public examples.

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

  1. {1, 4, 2, 5, 7}
    Returns: 4
    The example from the statement.
  2. {1, 5, 8, 3}
    Returns: -1
  3. {7}
    Returns: 7
    There are zero elements that are greater than 7 and zero elements that are smaller than 7.
  4. {7, 12}
    Returns: -1
  5. {66, 53, 47, 86, 18, 21, 97, 92, 15}
    Returns: 53
  6. {32, 54, 27, 4, 69, 96, 73, 1, 100, 15, 21}
    Returns: 32
Back to all problems