Day 4: Geometric Distribution II | 10 Days Of Statistics | HackerRank Solution

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

Day 4: Geometric Distribution II

Objective

In this challenge, we go further with geometric distributions. 

Task

The probability that a machine produces a defective product is 1/3. What is the probability that the 1st defect is found during the first 5 inspections?

Input Format

The first line contains the respective space-separated numerator and denominator for the probability of a defect, and the second line contains the inspection we want the probability of the first defect being discovered by:

1 3
5

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

Output Format

Print a single line denoting the answer, rounded to a scale of 3 decimal places (i.e., 1.234 format).

Solution – Geometric Distribution II

C++

#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 p = 0.333;
    double q = 1 - p;
    
    double g_prob = 0.0;
    
    for(auto i=1; i<=5; i++)
    {
        g_prob += pow(q, i-1)*p;
    }
    
    printf("%0.3f", g_prob);
    
    return 0;
}

Disclaimer: The above Problem (Geometric Distribution II) 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 *