
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Toss Strange Coins in C++
Suppose we have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. We have to show the probability that the number of coins facing heads equals target if you toss every coin exactly once. So if the prob array is like [0.5,0.5,0.5,0.5,0.5] and target is 0, then the output will be 0.03125.
To solve this, we will follow these steps −
- n := size of prob array
- create one 2d array of size n x (target + 5)
- set dp[0,0] = 1 – prob[0] and dp[0,1] := prob[0]
- for i in range 1 to n – 1
- dp[i, 0] := (1 – prob[i]) * dp[i – 1, 0]
- for j in range 1 to minimum of i + 1 and target
- dp[i, j] := (1 – prob[i]) * dp[i – 1, j] + prob[i]*dp[i – 1, j - 1]
- return dp[n – 1, target]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: double probabilityOfHeads(vector<double>& prob, int target) { int n = prob.size(); vector < vector <double> > dp(n, vector <double>(target+5)); dp[0][0] = 1- prob[0]; dp[0][1] = prob[0]; for(int i =1;i<n;i++){ dp[i][0] = (1-prob[i])*dp[i-1][0]; for(int j =1;j<=min(i+1,target);j++){ dp[i][j] = (1-prob[i])*dp[i-1][j] + prob[i]*dp[i-1][j-1]; } } return dp[n-1][target]; } }; main(){ vector<double> v = {0.5,0.5,0.5,0.5,0.5}; Solution ob; cout << (ob.probabilityOfHeads(v, 0)); }
Input
[0.5,0.5,0.5,0.5,0.5] 0
Output
0.03125
- Related Articles
- Strange Printer in C++
- Arranging Coins in C++
- Distribute Coins in Binary Tree in C++
- A Strange Wrestling Match
- Strange syntax, what does `?.` mean in JavaScript?
- Java Program to Toss a Coin
- 6 Strange Signs of Fibromyalgia
- Five coins were simultaneously tossed 1000 times and at each toss the number of heads were observed. The number of tosses during which 0, 1, 2, 3, 4 and 5 heads were obtained are shown in the table below. Find the mean number of heads per toss.
- 6 Strange Warning Signs of Low Testosterone
- C/C++ Program for Maximum height when coins are arranged in a triangle?
- Five coins were simultaneously tossed 1000 times, and at each toss the number of heads was observed. The number of tosses during which 0,1,2,3,4 and 5 heads were obtained are shown in the table below. Find the mean number of heads per toss.No. of heads per toss ($x$):012345No. of tosses ($f$):3814434228716425.
- Program to find maximum coins we can get from disappearing coins matrix in Python
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- Strange cursor placement in modal when using autofocus in Internet Explorer with HTML
- Probability of getting at least K heads in N tosses of Coins in C++

Advertisements