In this Hackerrank Day 6: The Central Limit Theorem III 10 Days of Statistics problem You have a sample of 100 values from a population with a mean of 500 and with a standard deviation of 80. Compute the interval that covers the middle 95% of the distribution of the sample mean; in other words, compute A and B such that P(A<x<B)=0.95. Use the value of z=1.96.


HackerRank Day 6: The Central Limit Theorem III | 10 Days of Statistics solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
# true population distribution
mu, sigma = 500, 80

# sample mean distribution
muS, sigmaS = mu, sigma/(100**0.5)

# confidence intervals of sample mean dist
A = mu - (1.96*sigmaS)
B = mu + (1.96*sigmaS)

print(round(A,2))
print(round(B,2))



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();
        float mean = scan.nextFloat();
        float sigma = scan.nextFloat();
        float goal = scan.nextFloat();
        float z = scan.nextFloat();
        
        float sampleSigma = sigma/(float)Math.sqrt(n);
        
        float A = mean - z*sampleSigma;
        float B = mean + z*sampleSigma;
        System.out.printf("%.2f\n",A);
        System.out.printf("%.2f\n",B);
    }  
    
}


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 */   
    double x1,x2;
    x1=-8*1.96+500;
    x2=8*1.96+500;
    printf("%.2f\n%.2f",x1,x2);
    return 0;
}


Problem solution in C programming.

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

int main() {

        double mean   = 500;
        double std    = 80;
        int    n      = 100;
        double zScore = 1.96; 
        
        double marginOfError =(double) (zScore * std) / (sqrt(n));

        /* Print output */
        printf("%.2f\n", mean - marginOfError);
        printf("%.2f\n", mean + marginOfError);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    input = input.split('\n')

    let mx = parseFloat(input[0]),
        m = parseFloat(input[1]),
        sd = parseFloat(input[2]),
        i = parseFloat(input[3]),
        z = parseFloat(input[4])

    function confidenceInterval(z, sd, mx) {
        return z * (sd / Math.sqrt(mx))
    }

    console.log((m - confidenceInterval(z, sd, mx)).toFixed(2))
    console.log((m + confidenceInterval(z, sd, mx)).toFixed(2))
} 

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

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