#K44747. Median Age Calculation

    ID: 27600 Type: Default 1000ms 256MiB

Median Age Calculation

Median Age Calculation

You are given a list of ages. Your task is to calculate the median age. First, sort the list in non-decreasing order. If the number of ages is odd, the median is the middle element. If the number of ages is even, then the median is defined as the smaller of the two middle values.

In mathematical terms, let \( n \) be the total number of ages and \( a_1, a_2, \dots, a_n \) be the ages sorted such that \( a_1 \le a_2 \le \cdots \le a_n \). The median \( M \) is given by:

  • If \( n \) is odd, \( M = a_{\left\lfloor \frac{n}{2} \right\rfloor + 1} \).
  • If \( n \) is even, \( M = a_{\frac{n}{2}} \) (i.e. the smaller of the two middle values).

For example, given the list [34, 28, 43, 57, 41], after sorting it becomes [28, 34, 41, 43, 57] and the median is 41.

inputFormat

The input is read from standard input (stdin). The first line contains an integer \( n \) which indicates the number of ages. The second line contains \( n \) space-separated integers, each representing an age.

Example:
5
34 28 43 57 41

outputFormat

Output the median age as defined in the problem statement to standard output (stdout).

Example:
41
## sample
5
34 28 43 57 41
41