Statistics

Interview

Time limit2sMemory limit256 MB

Summary
Given N odd integers, compute and print the rounded mean, median, mode (second smallest if tied), and range.
Level

Easy3 of 10

Topics
Array, Sorting, Math
Solved
No attempts yet

Problem

Processing integers is an important task in statistics. Given an odd number NN of integers, compute the following four basic statistics.

  1. Arithmetic mean: the sum of the NN numbers divided by NN, rounded to the nearest integer
  2. Median: the middle value after sorting the NN numbers in nondecreasing order
  3. Mode: the value that appears most often. If there is more than one, use the second smallest such value
  4. Range: the difference between the maximum and minimum values

Write a program that prints these four values for the given numbers, in order.

Input

The first line contains the number of values NN. (1≤N≤500 000)(1 \le N \le 500\,000)

NN is odd. Each of the next NN lines contains one integer. The absolute value of every integer is at most 4 0004\,000.

Output

On the first line, print the arithmetic mean, rounded to the nearest integer.

On the second line, print the median.

On the third line, print the mode. If there are multiple modes, print the second smallest one.

On the fourth line, print the range.

Examples4

  1. Example 1

    Input
    5
    1
    3
    8
    -2
    2
    
    Expected output
    2
    2
    1
    10
    
  2. Example 2

    Input
    1
    4000
    
    Expected output
    4000
    4000
    4000
    0
    
  3. Example 3

    Input
    5
    -1
    -2
    -3
    -1
    -2
    
    Expected output
    -2
    -2
    -1
    2
    
  4. Example 4

    Input
    3
    0
    0
    -1
    
    Expected output
    0
    0
    0
    1