Header Ad

HackerRank Day 0: Mean, Median, and Mode | 10 Days Of Statistics solution

In this Hackerrank Day 0: Mean, Median, and Mode 10 Days of Statistics problem we have Given an array of integers, calculate and print the respective mean, median, and mode on separate lines. If your array contains more than one modal value, choose the numerically smallest one.

HackerRank Day 0: Mean, Median, and Mode | 10 Days Of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import numpy as np
from scipy import stats

size = int(input())
numbers = list(map(int, input().split()))
print(np.mean(numbers))
print(np.median(numbers))
print(int(stats.mode(numbers)[0]))


Problem solution in Java Programming.

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 scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            int num = scan.nextInt();
            arr[i] = num;
            sum += num;
        }
        double mean = (double) sum / n;
        Arrays.sort(arr);
        double median;
        if(n % 2 == 0)
        {
            median = (arr[n / 2] + arr[n / 2 - 1]) / 2.0;
        }
        else
        {
            median = arr[n / 2];
        }
        int maxCount = 1;
        int count = 1;
        int current = arr[0];
        int mode = arr[0];
        for(int i = 1; i < arr.length; i++)
        {
            if(arr[i] == current)
            {
                count++;
            }
            else
            {
                count = 1;
                current = arr[i];
            }
            
            if(count > maxCount)
            {
                maxCount = count;
                mode = current;
            }
        }
        System.out.printf("%.1f\n%.1f\n%d", mean, median, mode);
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n, x;
    vector<int> v;
    cin >> n;
    while(n--)
    {
        cin >> x;
        v.push_back(x);
    }
    cout << ((float)accumulate(v.begin(), v.end(), 0))/v.size() << endl;
    sort(v.begin(), v.end());
    if (v.size() % 2 == 1)
        cout << v[(v.size() + 1) / 2] << endl;
    else
        cout << ((float)(v[v.size() / 2 - 1] + v[v.size() / 2]))/2   << endl;
    int mode = v[0];
    int max = 1;
    int currentMax = 1;
    for (int i = 1; i < v.size(); i++) {
        if (v[i] == v[i-1]) {
            currentMax++;
            if (currentMax > max) {
                max = currentMax;
                mode = v[i];
            }
        } else {
            currentMax = 1;
        }
    }
    cout << mode << endl;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int compare(const void *arg1, const void *arg2){
     return  (*(int *)arg1 - *(int *)arg2);
}

int main() {
    int n;
    scanf("%d\n", &n);
    int x[n];
    double mean = 0.0;
    for(int i = 0; i < n; i++)
    {    
        scanf("%d\n", &x[i]);
        mean += (double) x[i];
    }
    mean /= (double) n;
    printf("%0.1lf\n", mean);
    qsort((void *)x, n, sizeof(int), compare);    
    
    if(n % 2 == 1)
        printf("%d\n", x[(n+1)/2 - 1]);
    else
        printf("%0.1lf\n", (double)(x[n/2 - 1] + x[n/2+1-1])/2.0);
    
    int every_cnt[100000] = {0};
    int max_n = x[0];
    for(int i = 0; i < n; i++){
        every_cnt[x[i]]++;
    }
    
    for(int j = x[0]; j < 100000; j++)
    {
        if(every_cnt[j] == 0) 
            continue;
        if(every_cnt[j] > every_cnt[max_n])
            max_n = j;
    }    
        
    printf("%d\n", max_n);
    
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    input = input.split("\n")[1].split(" ").map(function(o){ return parseInt(o);});
    input.sort(function(a,b){ return a-b;});
    var f = [];
    var r = input.reduce(function(r, i){
        r.total = r.total + i;
        var d = f.find(function(o) {return o.val == i});
        if(d == null) {
            d = {val: i, freq: 1};
            f.push(d);
        } else {
            d.freq++;
        }
        return r;
    }, {total:0, len:input.length});
    
    f.sort(function(a,b){ 
        if(a.freq == b.freq) {
            return a.val - b.val;
        }
        return b.freq - a.freq;
    });
    console.log(r.total/r.len);
    console.log((input[r.len/2] + input[(r.len/2) - 1])/2);
    console.log(f[0].val);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Post a Comment

0 Comments