Day 4: Binomial Distribution I | 10 Days Of Statistics | HackerRank Solution

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

Day 4: Binomial Distribution I



In this challenge, we learn about binomial distributions. 


The ratio of boys to girls for babies born in Russia is 1.09 : 1. If there is 1 child born per birth, what proportion of Russian families with exactly 6 children will have at least 3 boys?

Write a program to compute the answer using the above parameters. Then print your result, rounded to a scale of 3 decimal places (i.e., 1.234 format).

Input Format

A single line containing the following values:

1.09 1

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 – Day 4: Binomial Distribution I


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

double b = 1.09, g = 1;  // given

int fact(int n)
    if(n<2) return 1;
    return n*fact(n-1);

double nCr(int n, int r)
    return fact(n)/(fact(r)*fact(n-r));

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    double p = b/(b+g);
    double q = 1 - p;
    double prob = 0.0;
    for(auto i=6; i>=3; i--)
        prob += nCr(6, i)*pow(p, i)*pow(q, 6-i);
    printf("%0.3f\n", prob);
    return 0;

Disclaimer: The above Problem (Binomial 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 *