Counts Path in an Array | Javascript implementation of the above approach ; Find the number of ways to reach the end ; Base case ; Recursive structure ; Driver code
let mod = 1000000000 ; function ways ( i , arr , n ) { if ( i == n - 1 ) return 1 ; let sum = 0 ; for ( let j = 1 ; j + i < n && j <= arr [ i ] ; j ++ ) { sum += ( ways ( i + j , arr , n ) ) % mod ; sum %= mod ; } return sum % mod ; } let arr = [ 5 , 3 , 1 , 4 , 3 ] ; let n = arr . length ; document . write ( ways ( 0 , arr , n ) ) ;