#C4293. Compute Statistical Measures
Compute Statistical Measures
Compute Statistical Measures
Given a list of integers, compute the following statistical measures: mean, median, mode, variance, and standard deviation. Use the formulas below:
- Mean: $$\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i$$
- Median: the middle value when the list is sorted. If there are an even number of values, then $$median = \frac{x_{\frac{n}{2}} + x_{\frac{n}{2}+1}}{2}$$.
- Mode: the smallest value among those that occur most frequently.
- Variance: $$\sigma^2 = \frac{1}{n}\sum_{i=1}^{n} (x_i - \bar{x})^2$$
- Standard Deviation: $$\sigma = \sqrt{\sigma^2}$$
If the list is empty, then all statistics should be reported as null
.
inputFormat
The input is given through stdin:
- The first line contains a single integer
n
which represents the number of integers. - If
n > 0
, the second line containsn
space-separated integers.
outputFormat
Output a JSON object (printed to stdout) with the following keys: mean
, median
, mode
, variance
, and std_deviation
. For an empty list (n = 0
), all values should be null
. Otherwise, output the computed statistical measures. For example:
{ "mean": 2.5, "median": 2.5, "mode": 1, "variance": 1.25, "std_deviation": 1.118033988749895 }## sample
0
{"mean": null, "median": null, "mode": null, "variance": null, "std_deviation": null}