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.32
Explanation :-
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++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   floatfind_prob(intN, floatP)
 {
     doubledp[N + 1];
     dp[0] = 1;
     dp[1] = 0;
     dp[2] = P;
     dp[3] = 1 - P;
     for(inti = 4; i <= N; ++i)
         dp[i] = (P)*dp[i - 2] + (1 - P) * dp[i - 3];
       returndp[N];
 }
   intmain()
 {
     intn = 5;
     floatp = 0.2;
     cout << find_prob(n, p);
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.io.*;
   classGFG {
     
     
     staticfloatfind_prob(intN, floatP)
     {
         doubledp[] = newdouble[N + 1];
         dp[0] = 1;
         dp[1] = 0;
         dp[2] = P;
         dp[3] = 1- P;
     
         for(inti = 4; i <= N; ++i)
           dp[i] = (P) * dp[i - 2] +
                         (1- P) * dp[i - 3];
     
         return((float)(dp[N]));
     }
     
     
     publicstaticvoidmain(String args[])
     {
         intn = 5;
         floatp = 0.2f;
         System.out.printf("%.2f",find_prob(n, p));
     }
 }
     | 
 
 
 
 
Python3
| 
  deffind_prob(N, P) :
     
     dp =[0] *(n +1)
     dp[0] =1
     dp[1] =0
     dp[2] =P
     dp[3] =1-P
     
     fori inrange(4, N +1) :
         dp[i] =(P) *dp[i -2] +(1-P) *dp[i -3]
       returndp[N]
   n =5
 p =0.2
 print(round(find_prob(n, p), 2))
   | 
 
 
 
 
C#
| 
usingSystem;
   classGFG {
     
     
     staticfloatfind_prob(intN, floatP)
     {
         double[]dp = newdouble[N + 1];
         dp[0] = 1;
         dp[1] = 0;
         dp[2] = P;
         dp[3] = 1 - P;
     
         for(inti = 4; i <= N; ++i)
         dp[i] = (P) * dp[i - 2] +
                 (1 - P) * dp[i - 3];
     
         return((float)(dp[N]));
     }
     
     
     publicstaticvoidMain()
     {
         intn = 5;
         floatp = 0.2f;
         Console.WriteLine(find_prob(n, p));
     }
 }
     | 
 
 
 
 
Javascript
| 
<script>
        
     functionfind_prob(N, P)
     {
         let dp = [];
         dp[0] = 1;
         dp[1] = 0;
         dp[2] = P;
         dp[3] = 1 - P;
       
         for(let i = 4; i <= N; ++i)
           dp[i] = (P) * dp[i - 2] +
                         (1 - P) * dp[i - 3];
       
         return(dp[N]);
     }
           let n = 5;
         let p = 0.2;
         document.write(find_prob(n, p));
       
       
 </script>
 | 
 
 
 
 
PHP
| 
<?php
   functionfind_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];
 }
   $n= 5;
 $p= 0.2;
 echofind_prob($n, $p);
   ?>
 | 
 
 
 
 
 
Time Complexity: O(n)
Auxiliary Space: O(n)
 Efficient approach: Space optimization O(1)
In the previous approach, the current value dp[i] only depends on the previous 2 values of dp i.e. dp[i-2] and dp[i-3]. So to optimize the space complexity we can store the previous 4 values of Dp in 4 variables  his way, the space complexity will be reduced from O(N) to O(1)
Implementation Steps:
- Initialize variables for dp[0], dp[1], dp[2], and dp[3] as 1, 0, P, and 1-P respectively.
- Iterate from i = 4 to N and use the formula dp[i] = (P)*dp[i – 2] + (1 – P) * dp[i – 3] to compute the current value of dp.
- After each iteration, update the values of dp0, dp1, dp2, and dp3 to dp1, dp2, dp3, and curr respectively.
- Return the final value of curr.
Implementation:
C++
| 
#include <bits/stdc++.h>
 usingnamespacestd;
   floatfind_prob(intN, floatP)
 {    
       
     doublecurr;
     
       
     doubledp0 = 1, dp1=0, dp2=P, dp3= 1-P;
     
       
       
     for(inti = 4; i <= N; ++i){
         curr = (P)*dp2 + (1 - P) * dp1;
       
           
         dp0=dp1;
         dp1=dp2;
         dp2=dp3;
         dp3=curr;
     }
     
       
     returncurr;
 }
   intmain()
 {
     intn = 5;
     floatp = 0.2;
     cout << find_prob(n, p);
     return0;
 }
 | 
 
 
 
 
Java
| 
importjava.util.*;
   publicclassMain {
       
     staticfloatfind_prob(intN, floatP) {
           
         doublecurr;
           
         doubledp0 = 1, dp1 = 0, dp2 = P, dp3 = 1- P;
           
         
         for(inti = 4; i <= N; ++i) {
             curr = (P) * dp2 + (1- P) * dp1;
               
             dp0 = dp1;
             dp1 = dp2;
             dp2 = dp3;
             dp3 = curr;
         }
           
         return(float) curr;
     }
       
     publicstaticvoidmain(String[] args) {
         intn = 5;
         floatp = 0.2f;
         System.out.println(find_prob(n, p));
     }
 }
 | 
 
 
 
 
Python3
| 
deffind_prob(N, P):
     
     curr =0.0
       
     dp0, dp1, dp2, dp3 =1.0, 0.0, P, 1-P
       
     
     fori inrange(4, N +1):
         curr =P *dp2 +(1-P) *dp1
           
         dp0, dp1, dp2, dp3 =dp1, dp2, dp3, curr
       
     returnround(curr, 2)
   if__name__ =="__main__":
     n =5
     p =0.2
     print(find_prob(n, p))
 | 
 
 
 
 
Javascript
| 
functionfind_prob(N, P) {    
   
   let curr;
   
   
   let dp0 = 1, dp1 = 0, dp2 = P, dp3 = 1 - P;
   
   
   
   for(let i = 4; i <= N; ++i) {
     curr = (P * dp2) + ((1 - P) * dp1);
     
     
     dp0 = dp1;
     dp1 = dp2;
     dp2 = dp3;
     dp3 = curr;
   }
   
   
   returncurr;
 }
   let n = 5;
 let p = 0.2;
 console.log(find_prob(n, p).toFixed(2));
 | 
 
 
 
 
 
Time complexity: O(N)
Auxiliary Space: O(1)
                                            Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
                                            Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!