TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

OrderStatistic

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a list of numbers, the ith order statistic is the ith highest number. The 1st highest number is the maximum element in the list. The 2nd highest number is the second largest element in the list... Given a int[] numbers, and an int i you will return the ith order statistic of numbers. To make things simpler, i will be either 1 or 2. In other words, you must either find the largest number (i = 1) or the second largest number (i = 2).

Definition

Class:
OrderStatistic
Method:
getStat
Parameters:
int[], int
Returns:
int
Method signature:
int getStat(int[] numbers, int i)
(be sure your method is public)

Constraints

  • numbers will contain between 2 and 50 elements inclusive
  • Each element of numbers will be between -1000 and 1000 inclusive
  • Each element of numbers will be distinct
  • i will be either 1 or 2.

Examples

  1. {7,2,8,-10}
    1
    Returns: 8
    8 is the maximum element.
  2. {7,2,8,-10}
    2
    Returns: 7
    7 is the 2nd highest element
  3. {-1,-2,-3,-4,-5}
    1
    Returns: -1
  4. {0,1,2,3,4,5}
    2
    Returns: 4
Back to all problems