#C14464. Calculate the Median
Calculate the Median
Calculate the Median
You are given a list of integers. Your task is to compute the median of the list.
The input consists of two parts: the first line contains an integer n denoting the number of elements in the list, and the second line contains n space-separated integers.
If the list is empty (i.e. n = 0), output None
. When the count of numbers is odd, the median is the middle element after sorting the list. When it is even, the median is defined as the average of the two middle elements. The answer should be printed as an integer if it is whole, or as a decimal number otherwise.
The formulas used are as follows:
\[ \text{If } n \text{ is odd: } \text{median} = A_{\left( \frac{n+1}{2} \right)} \]
\[ \text{If } n \text{ is even: } \text{median} = \frac{A_{\frac{n}{2}} + A_{\frac{n}{2}+1}}{2} \]
inputFormat
The first line contains an integer n
(number of elements).
The second line contains n
space-separated integers. If n
is 0, the second line will be empty.
outputFormat
Output the median value. If the list is empty, print None
. If the median is fractional, print its decimal representation; if it is a whole number, print it as an integer.
5
1 3 4 2 5
3