
- 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
Find the probability of reaching all points after N moves from point N in C++
Suppose we have a number N this represents the initial position of the person on the number line. We also have L which is the probability of the person of going left. We have to find the the probability of reaching all points on the number line after completing N moves from point N. Each move can be either to the left or to the right.
So, if the input is like n = 2, l = 0.5, then the output will be [0.25, 0, 0.5, 0, 0.25]
To solve this, we will follow these steps −
high := 1 - low
Define an array A of size: n+1 x 2*n+1 and fill with 0
A[1, n + 1] = high, A[1, n - 1] = low
for initialize i := 2, when i <= n, update (increase i by 1), do −
for initialize j := 1, when j −= 2 * n, update (increase j by 1), do −
A[i, j] := A[i, j] + (A[i - 1, j - 1] * high)
for initialize j := 2 * n - 1, when j >= 0, update (decrease j by 1), do −
A[i, j] := A[i, j] + (A[i - 1, j + 1] * low)
for initialize i := 0, when i − 2*n+1, update (increase i by 1), do −
display A[n, i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void find_prob(int n, double low) { double high = 1 - low; double A[n + 1][2 * n + 1] = {{0}}; A[1][n + 1] = high; A[1][n - 1] = low; for (int i = 2; i <= n; i++) { for (int j = 1; j <= 2 * n; j++) A[i][j] += (A[i - 1][j - 1] * high); for (int j = 2 * n - 1; j >= 0; j--) A[i][j] += (A[i - 1][j + 1] * low); } for (int i = 0; i < 2*n+1; i++) cout << A[n][i] << endl; } int main() { int n = 2; double low = 0.6; find_prob(n, low); }
Input
2, 0.6
Output
0.36 0 0.48 0 0.16
- Related Articles
- Find the Number of Triangles After N Moves using C++
- Count number of 1s in the array after N moves in C
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Reaching Points in C++
- Probability of reaching a point with 2 or 3 steps at a time in C++
- Maximizing Probability of one type from N containers in C++
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++\n
- C++ Program to find out the moves to read a point from another point in a 2D plane
- Sum of the digits of a number N written in all bases from 2 to N/2 in C++
- Program to find all missing numbers from 1 to N in Python
- Find the sum of all Truncatable primes below N in Python
- Get n numbers from array starting from given point JavaScript
- Find x from the following:"\n
- Find all the angles in the figure given below."\n
- In the figure, JKLM is a square with sides of length 6 units. Points A and B are the mid-points of sides KL and LM respectively. If a point is selected at random from the interior of the square. What is the probability that the point will be chosen from the interior of $\triangle JAB$?"\n
