A person starts walking from position X = 0, find the probability to reach exactly on X = N if she can only take either 2 steps or 3 steps. Probability for step length 2 is given i.e. P, probability for step length 3 is 1 – P.

**Examples :**

Input : N = 5, P = 0.20 Output : 0.32Explanation:- There are two ways to reach 5. 2+3 with probability = 0.2 * 0.8 = 0.16 3+2 with probability = 0.8 * 0.2 = 0.16 So, total probability = 0.32.

It is a simple dynamic programming problem. It is simple extension of this problem :- count-ofdifferent-ways-express-n-sum-1-3-4

Below is the implementation of the above approach.

## C++

`// CPP Program to find probability to ` `// reach N with P probability to take ` `// 2 steps (1-P) to take 3 steps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns probability to reach N ` `float` `find_prob(` `int` `N, ` `float` `P) ` `{ ` ` ` `double` `dp[N + 1]; ` ` ` `dp[0] = 1; ` ` ` `dp[1] = 0; ` ` ` `dp[2] = P; ` ` ` `dp[3] = 1 - P; ` ` ` `for` `(` `int` `i = 4; i <= N; ++i) ` ` ` `dp[i] = (P)*dp[i - 2] + (1 - P) * dp[i - 3]; ` ` ` ` ` `return` `dp[N]; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `float` `p = 0.2; ` ` ` `cout << find_prob(n, p); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find probability to ` `// reach N with P probability to take ` `// 2 steps (1-P) to take 3 steps ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns probability to reach N ` ` ` `static` `float` `find_prob(` `int` `N, ` `float` `P) ` ` ` `{ ` ` ` `double` `dp[] = ` `new` `double` `[N + ` `1` `]; ` ` ` `dp[` `0` `] = ` `1` `; ` ` ` `dp[` `1` `] = ` `0` `; ` ` ` `dp[` `2` `] = P; ` ` ` `dp[` `3` `] = ` `1` `- P; ` ` ` ` ` `for` `(` `int` `i = ` `4` `; i <= N; ++i) ` ` ` `dp[i] = (P) * dp[i - ` `2` `] + ` ` ` `(` `1` `- P) * dp[i - ` `3` `]; ` ` ` ` ` `return` `((` `float` `)(dp[N])); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `float` `p = ` `0` `.2f; ` ` ` `System.out.printf(` `"%.2f"` `,find_prob(n, p)); ` ` ` `} ` `} ` ` ` ` ` `/* This code is contributed by Nikita Tiwari.*/` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find ` `# probability to reach N with ` `# P probability to take 2 ` `# steps (1-P) to take 3 steps ` ` ` `# Returns probability to reach N ` `def` `find_prob(N, P) : ` ` ` ` ` `dp ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` `dp[` `0` `] ` `=` `1` ` ` `dp[` `1` `] ` `=` `0` ` ` `dp[` `2` `] ` `=` `P ` ` ` `dp[` `3` `] ` `=` `1` `-` `P ` ` ` ` ` `for` `i ` `in` `range` `(` `4` `, N ` `+` `1` `) : ` ` ` `dp[i] ` `=` `(P) ` `*` `dp[i ` `-` `2` `] ` `+` `(` `1` `-` `P) ` `*` `dp[i ` `-` `3` `] ` ` ` ` ` `return` `dp[N] ` ` ` `# Driver code ` `n ` `=` `5` `p ` `=` `0.2` `print` `(` `round` `(find_prob(n, p), ` `2` `)) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find probability to ` `// reach N with P probability to take ` `// 2 steps (1-P) to take 3 steps ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns probability to reach N ` ` ` `static` `float` `find_prob(` `int` `N, ` `float` `P) ` ` ` `{ ` ` ` `double` `[]dp = ` `new` `double` `[N + 1]; ` ` ` `dp[0] = 1; ` ` ` `dp[1] = 0; ` ` ` `dp[2] = P; ` ` ` `dp[3] = 1 - P; ` ` ` ` ` `for` `(` `int` `i = 4; i <= N; ++i) ` ` ` `dp[i] = (P) * dp[i - 2] + ` ` ` `(1 - P) * dp[i - 3]; ` ` ` ` ` `return` `((` `float` `)(dp[N])); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `float` `p = 0.2f; ` ` ` `Console.WriteLine(find_prob(n, p)); ` ` ` `} ` `} ` ` ` ` ` `/* This code is contributed by vt_m.*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find probability to ` `// reach N with P probability to take ` `// 2 steps (1-P) to take 3 steps ` ` ` `// Returns probability to reach N ` `function` `find_prob(` `$N` `, ` `$P` `) ` `{ ` ` ` `$dp` `; ` ` ` `$dp` `[0] = 1; ` ` ` `$dp` `[1] = 0; ` ` ` `$dp` `[2] = ` `$P` `; ` ` ` `$dp` `[3] = 1 - ` `$P` `; ` ` ` `for` `(` `$i` `= 4; ` `$i` `<= ` `$N` `; ++` `$i` `) ` ` ` `$dp` `[` `$i` `] = (` `$P` `) * ` `$dp` `[` `$i` `- 2] + ` ` ` `(1 - ` `$P` `) * ` `$dp` `[` `$i` `- 3]; ` ` ` ` ` `return` `$dp` `[` `$N` `]; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$p` `= 0.2; ` `echo` `find_prob(` `$n` `, ` `$p` `); ` ` ` `// This code is contributed by mits. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

0.32

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the probability of reaching all points after N moves from point N
- Reaching a point using clockwise or anticlockwise movements
- Minimum time to reach a point with +t and -t moves at time t
- Minimum steps to come back to starting point in a circular tour
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Generate integer from 1 to 7 with equal probability
- Random number generator in arbitrary probability distribution fashion
- Probability of Knight to remain in the chessboard
- Probability of getting at least K heads in N tosses of Coins
- Aptitude | Probability | Question 1
- Aptitude | Probability | Question 2
- Aptitude | Probability | Question 3
- Aptitude | Probability | Question 4
- Aptitude | Probability | Question 5
- Aptitude | Probability | Question 6
- Aptitude | Probability | Question 7
- Aptitude | Probability | Question 8

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.