# Count ways to reach end from start stone with at most K jumps at each step

Given **N** stones in a row from left to right. From each stone, you can jump to at most **K** stones. The task is to find the total number of ways to reach from **sth** stone to **Nth** stone.

**Examples:**

Input:N = 5, s = 2, K = 2

Output:Total Ways = 3

Explanation:

Assume s1, s2, s3, s4, s5 be the stones. The possible paths from 2nd stone to 5th stone:

s2 -> s3 -> s4 -> s5

s2 -> s4 -> s5

s2 -> s3 -> s5

Hence total number of ways = 3

Input:N = 8, s = 1, K = 3

Output:Total Ways = 44

**Approach:**

- Let assume
**dp[i]**be the number of ways to reach**ith**stone. - Since there are atmost
**K jumps**, So the**ith**stone can be reach by all it’s previous**K**stones. - Iterate for all possible
**K jumps**and keep adding this possible combination to the array**dp[]**. - Then the total number of possible ways to reach
**Nth**node from**sth**stone will be**dp[N-1]**. - For Example:

Let N = 5, s = 2, K = 2, then we have to reach Nth stone from sth stone.

Let dp[N+1] is the array that stores the number of paths to reach the Nth Node from sth stone.

Initially, dp[] = { 0, 0, 0, 0, 0, 0 } and dp[s] = 1, then

dp[] = { 0, 0, 1, 0, 0, 0 }

To reach the 3rd,

There is only 1 way with at most 2 jumps i.e., from stone 2(with jump = 1). Update dp[3] = dp[2]

dp[] = { 0, 0, 1, 1, 0, 0 }To reach the 4th stone,

The two ways with at most 2 jumps i.e., from stone 2(with jump = 2) and stone 3(jump = 1). Update dp[4] = dp[3] + dp[2]

dp[] = { 0, 0, 1, 1, 2, 0 }To reach the 5th stone,

The two ways with at most 2 jumps i.e., from stone 3(with jump = 2) and stone 4(with jump = 1). Update dp[5] = dp[4] + dp[3]

dp[] = { 0, 0, 1, 1, 2, 3 }Now dp[N] = 3 is the number of ways to reach Nth stone from sth stone.

Below is the implementation of the above approach:

## C++

`// C++ program to find total no.of ways ` `// to reach nth step ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Function which returns total no.of ways ` `// to reach nth step from sth steps ` `int` `TotalWays(` `int` `n, ` `int` `s, ` `int` `k) ` `{ ` ` ` `// Initialize dp array ` ` ` `int` `dp[n]; ` ` ` ` ` `// filling all the elements with 0 ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// Initialize (s-1)th index to 1 ` ` ` `dp[s - 1] = 1; ` ` ` ` ` `// Iterate a loop from s to n ` ` ` `for` `(` `int` `i = s; i < n; i++) { ` ` ` ` ` `// starting range for counting ranges ` ` ` `int` `idx = max(s - 1, i - k); ` ` ` ` ` `// Calculate Maximum moves to ` ` ` `// Reach ith step ` ` ` `for` `(` `int` `j = idx; j < i; j++) { ` ` ` `dp[i] += dp[j]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// For nth step return dp[n-1] ` ` ` `return` `dp[n - 1]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// no of steps ` ` ` `int` `n = 5; ` ` ` ` ` `// Atmost steps allowed ` ` ` `int` `k = 2; ` ` ` ` ` `// starting range ` ` ` `int` `s = 2; ` ` ` `cout << ` `"Total Ways = "` ` ` `<< TotalWays(n, s, k); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find total no.of ways ` `// to reach nth step ` `class` `GFG{ ` ` ` `// Function which returns total no.of ways ` `// to reach nth step from sth steps ` `static` `int` `TotalWays(` `int` `n, ` `int` `s, ` `int` `k) ` `{ ` ` ` `// Initialize dp array ` ` ` `int` `[]dp = ` `new` `int` `[n]; ` ` ` ` ` `// Initialize (s-1)th index to 1 ` ` ` `dp[s - ` `1` `] = ` `1` `; ` ` ` ` ` `// Iterate a loop from s to n ` ` ` `for` `(` `int` `i = s; i < n; i++) { ` ` ` ` ` `// starting range for counting ranges ` ` ` `int` `idx = Math.max(s - ` `1` `, i - k); ` ` ` ` ` `// Calculate Maximum moves to ` ` ` `// Reach ith step ` ` ` `for` `(` `int` `j = idx; j < i; j++) { ` ` ` `dp[i] += dp[j]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// For nth step return dp[n-1] ` ` ` `return` `dp[n - ` `1` `]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// no of steps ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `// Atmost steps allowed ` ` ` `int` `k = ` `2` `; ` ` ` ` ` `// starting range ` ` ` `int` `s = ` `2` `; ` ` ` `System.out.print(` `"Total Ways = "` ` ` `+ TotalWays(n, s, k)); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find total no.of ways ` `# to reach nth step ` ` ` `# Function which returns total no.of ways ` `# to reach nth step from sth steps ` `def` `TotalWays(n, s, k): ` ` ` ` ` `# Initialize dp array ` ` ` `dp ` `=` `[` `0` `]` `*` `n ` ` ` ` ` `# Initialize (s-1)th index to 1 ` ` ` `dp[s ` `-` `1` `] ` `=` `1` ` ` ` ` `# Iterate a loop from s to n ` ` ` `for` `i ` `in` `range` `(s, n): ` ` ` ` ` `# starting range for counting ranges ` ` ` `idx ` `=` `max` `(s ` `-` `1` `, i ` `-` `k) ` ` ` ` ` `# Calculate Maximum moves to ` ` ` `# Reach ith step ` ` ` `for` `j ` `in` `range` `( idx, i) : ` ` ` `dp[i] ` `+` `=` `dp[j] ` ` ` ` ` `# For nth step return dp[n-1] ` ` ` `return` `dp[n ` `-` `1` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `# no of steps ` ` ` `n ` `=` `5` ` ` ` ` `# Atmost steps allowed ` ` ` `k ` `=` `2` ` ` ` ` `# starting range ` ` ` `s ` `=` `2` ` ` `print` `(` `"Total Ways = "` `, TotalWays(n, s, k)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find total no.of ways ` `// to reach nth step ` `using` `System; ` ` ` `class` `GFG{ ` ` ` ` ` `// Function which returns total no.of ways ` ` ` `// to reach nth step from sth steps ` ` ` `static` `int` `TotalWays(` `int` `n, ` `int` `s, ` `int` `k) ` ` ` `{ ` ` ` `// Initialize dp array ` ` ` `int` `[]dp = ` `new` `int` `[n]; ` ` ` ` ` `// Initialize (s-1)th index to 1 ` ` ` `dp[s - 1] = 1; ` ` ` ` ` `// Iterate a loop from s to n ` ` ` `for` `(` `int` `i = s; i < n; i++) { ` ` ` ` ` `// starting range for counting ranges ` ` ` `int` `idx = Math.Max(s - 1, i - k); ` ` ` ` ` `// Calculate Maximum moves to ` ` ` `// Reach ith step ` ` ` `for` `(` `int` `j = idx; j < i; j++) { ` ` ` `dp[i] += dp[j]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// For nth step return dp[n-1] ` ` ` `return` `dp[n - 1]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `// no of steps ` ` ` `int` `n = 5; ` ` ` ` ` `// Atmost steps allowed ` ` ` `int` `k = 2; ` ` ` ` ` `// starting range ` ` ` `int` `s = 2; ` ` ` `Console.Write(` `"Total Ways = "` `+ TotalWays(n, s, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

Total Ways = 3

**Time Complexity:** O(N^{2}), where N is the number of stones.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach the Nth stair using any step from the given array
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Find the number of ways to reach Kth step in stair case
- Count minimum factor jumps required to reach the end of an Array
- Count ways to reach the n'th stair
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to jump to reach end
- Count number of ways to reach a given score in a game
- Count number of ways to reach destination in a Maze
- Count number of ways to reach destination in a maze
- Count number of ways to reach a given score in a Matrix
- Minimum number of jumps to reach end
- Minimum jumps to reach last building in a matrix
- Minimum number of Fibonacci jumps to reach end
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Reach the numbers by making jumps of two given lengths
- Check if it is possible to reach a number by making jumps of two given length
- Minimize steps to reach K from 0 by adding 1 or doubling at each step

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.