Objective In this challenge, we practice calculating the  mean ,  median , and  mode . Check out the  Tutorial  tab for learning materials...

Day 0: Mean, Median, and Mode Solution

 Objective

In this challenge, we practice calculating the meanmedian, and mode. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given an array, , of  integers, calculate and print the respective meanmedian, and mode on separate lines. If your array contains more than one modal value, choose the numerically smallest one.

Note: Other than the modal value (which will always be an integer), your answers should be in decimal form, rounded to a scale of  decimal place (i.e.,  format).

Example

The mean is .
The median is .
The mode is  because  occurs most frequently.

Input Format

The first line contains an integer, , the number of elements in the array.
The second line contains  space-separated integers that describe the array's elements.

Constraints

  • , where  is the  element of the array.

Output Format

Print  lines of output in the following order:

  1. Print the mean on the first line to a scale of  decimal place (i.e., ).
  2. Print the median on a new line, to a scale of  decimal place (i.e., ).
  3. Print the mode on a new line. If more than one such value exists, print the numerically smallest one.

Sample Input

10
64630 11735 14216 99233 14470 4978 73429 38120 51135 67060

Sample Output

43900.6
44627.5
4978

Explanation

Mean:
We sum all  elements in the array, divide the sum by , and print our result on a new line.

Median:
To calculate the median, we need the elements of the array to be sorted in either non-increasing or non-decreasing order. The sorted array . We then average the two middle elements:

and print our result on a new line.

Mode:
We can find the number of occurrences of all the elements in the array:

 4978 : 1
11735 : 1
14216 : 1
14470 : 1
38120 : 1
51135 : 1
64630 : 1
67060 : 1
73429 : 1
99233 : 1

Every number occurs once, making  the maximum number of occurrences for any number in . Because we have multiple values to choose from, we want to select the smallest one, , and print it on a new line.


Solution

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        List<Integer> ar = new ArrayList<>();

        int n = sc.nextInt();
        int t = n, m = 0, max = -1;
        double mode = 0.0, mean = 0.0, median = 0.0, sum = 0.0;

        while (t-- > 0) {
            ar.add(sc.nextInt());
        }
        for (Integer x : ar)
            sum += x;

        mean = sum / n;
        System.out.printf("%.1f\n", mean);
        Collections.sort(ar);

        if (n % 2 == 0) {
            median = ((double)ar.get(n / 2) + (double)ar.get(n / 2 - 1)) / 2;
        } else
            median = ar.get(n / 2 + 1);

        System.out.printf("%.1f\n", median);

        mode = ar.get(0);
        
        for (int i = 0; i < ar.size() - 1; i++) {
            if ((int)ar.get(i) == (int)ar.get(i + 1)) {
                m++;
                if (m > max) {
                    max = m;
                    mode = ar.get(i);
                    m = 0;
                }
            } else {
                m=0;
            }
        }
        System.out.println((int) mode);
    }
}

0 comments:

Do not spam here.