Hello coders, today we are going to solve Day 4: Geometric Distribution HackerRank Solution which is a Part of 10 Days of Statistics Series.
Objective
In this challenge, we learn about geometric distributions.
Task
The probability that a machine produces a defective product is 1/3. What is the probability that the 1st defect occurs the 5th item produced?
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 being the first defect for:
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 I
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.0 - p; //4 = 5-1; double g_prob = pow(q, 4)*p; printf("%0.3f", g_prob); return 0; }
Disclaimer: The above Problem (Geometric Distribution I) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.