Count ways to reach end from start stone with at most K jumps at each step | Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; filling all the elements with 0 ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; no of steps ; Atmost steps allowed ; starting range
function TotalWays ( n , s , k ) { let dp = new Array ( n ) ; dp . fill ( 0 ) ; dp [ s - 1 ] = 1 ; for ( let i = s ; i < n ; i ++ ) { let idx = Math . max ( s - 1 , i - k ) ; for ( let j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } let n = 5 ; let k = 2 ; let s = 2 ; document . write ( " " + TotalWays ( n , s , k ) ) ;