Day 5: Normal Distribution I | 10 Days Of Statistics | HackerRank Solution

Hello coders, today we are going to solve Day 5: Normal Distribution I HackerRank Solution which is a Part of 10 Days of Statistics Series.

Day 5: Normal Distribution I

Objective

In this challenge, we learn about normal distributions.

Task

In a certain plant, the time taken to assemble a car is a random variable, X, having a normal distribution with a mean of 20 hours and a standard deviation of 2 hours. What is the probability that a car can be assembled at this plant in:

  1. Less than 19.5 hours?
  2. Between 20 and 22 hours?

Input Format

There are 3 lines of input (shown below):

20 2
19.5
20 22

The first line contains 2 space-separated values denoting the respective mean and standard deviation for X. The second line contains the number associated with question 1. The third line contains 2 space-separated values describing the respective lower and upper range boundaries for question 2.

If you do not wish to read this information from stdin, you can hard-code it into your program.

Output Format

There are two lines of output. Your answers must be rounded to a scale of 3 decimal places (i.e., 1.234 format):

  1. On the first line, print the answer to question 1 (i.e., the probability that a car can be assembled in less than 19.5 hours).
  2. On the second line, print the answer to question 2 (i.e., the probability that a car can be assembled in between 20 to 22 hours).

Solution – Normal Distribution I

C++

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


double normal_dist(double m, double sd, double x)
{
    
    double p = 0.5*(1 + erf((x-m)/(sd*sqrt(2.0))));
    return p;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    
    double m = 20, sd = 2, x = 19.5, a = 20, b = 22;
    
    double p1 = normal_dist(m, sd, x);
    double p2 = normal_dist(m, sd, b) - normal_dist(m, sd, a);
    
    printf("%0.3f\n%0.3f", p1, p2);
    
    return 0;
}

Disclaimer: The above Problem (Normal Distribution I) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *