Probability of reaching a point with 2 or 3 steps at a time | Returns probability to reach N ; Driver Code
function find_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 ) ) ;