# Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)

• Difficulty Level : Medium
• Last Updated : 28 Mar, 2022

A monkey is standing below at a staircase having N steps. Considering it can take a leap of 1 to N steps at a time, calculate how many ways it can reach the top of the staircase?

Examples:

```Input : 2
Output : 2
It can either take (1, 1) or (2) to
reach the top. So, total 2 ways

Input : 3
Output : 4
Possibilities : (1, 1, 1), (1, 2), (2, 1),
(3). So, total 4 ways ```

There are 3 different ways to think of the problem.

1. In all possible solutions, a step is either stepped on by the monkey or can be skipped. So using the fundamental counting principle, the first step has 2 ways to take part, and for each of these, 2nd step also has 2 ways, and so on. but the last step always has to be stepped on.
```  2 x 2 x 2 x .... x 2(N-1 th step) x 1(Nth step)
= 2(N-1) different ways. ```
1. Let’s define a function F(n) for the use case. F(n) denotes all possible way to reach from bottom to top of a staircase having N steps, where min leap is 1 step and max leap is N step. Now, for the monkey, the first move it can make is possible in N different ways ( 1 step, 2 steps, 3 steps .. N steps). If it takes the first leap as 1 step, it will be left with N-1 more steps to conquer, which can be achieved in F(N-1) ways. And if it takes the first leap as 2 steps, it will have N-2 steps more to cover, which can be achieved in F(N-2) ways. Putting together,
```F(N) = F(N-1) + F(N-2) + F(N-3) + ... +
F(2) + F(1) + F(0)
Now,
F(0) = 1
F(1) = 1
F(2) = 2
F(3) = 4

Hence,
F(N) = 1 + 1 + 2 + 4 + ... + F(n-1)
= 1 + 2^0 + 2^1 + 2^2 + ... + 2^(n-2)
= 1 + [2^(n-1) - 1]```

## C++

 `// C++ program to count total number of ways``// to reach n-th stair with all jumps allowed``#include ` `int` `calculateLeaps(``int` `n)``{``    ``if` `(n == 0 || n == 1) {``        ``return` `1;``    ``}``    ``else` `{``        ``int` `leaps = 0;``        ``for` `(``int` `i = 0; i < n; i++)``            ``leaps += calculateLeaps(i);``        ``return` `leaps;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `calculateLeaps(``int``);``    ``std::cout << calculateLeaps(4) << std::endl;``    ``return` `0;``}`

## Java

 `// Java program to count total number of ways``// to reach n-th stair with all jumps allowed``class` `GFG {``    ``static` `int` `calculateLeaps(``int` `n)``    ``{``        ``if` `(n == ``0` `|| n == ``1``) {``            ``return` `1``;``        ``}``        ``else` `{``            ``int` `leaps = ``0``;``            ``for` `(``int` `i = ``0``; i < n; i++)``                ``leaps += calculateLeaps(i);``            ``return` `leaps;``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``System.out.println(calculateLeaps(``4``));``    ``}``}``// This code is contributed by Anant Agarwal.`

## Python3

 `# Python program to count``# total number of ways``# to reach n-th stair with``# all jumps allowed` `def` `calculateLeaps(n):``    ``if` `n ``=``=` `0` `or` `n ``=``=` `1``:``        ``return` `1``;``    ``else``:``        ``leaps ``=` `0``;``        ``for` `i ``in` `range``(``0``,n):``            ``leaps ``=` `leaps ``+` `calculateLeaps(i);``        ``return` `leaps;` `# Driver code``print``(calculateLeaps(``4``));` `# This code is contributed by mits`

## C#

 `// C# program to count total number of ways``// to reach n-th stair with all jumps allowed``using` `System;` `class` `GFG {` `    ``// Function to calculate leaps``    ``static` `int` `calculateLeaps(``int` `n)``    ``{``        ``if` `(n == 0 || n == 1) {``            ``return` `1;``        ``}``        ``else` `{``            ``int` `leaps = 0;``            ``for` `(``int` `i = 0; i < n; i++)``                ``leaps += calculateLeaps(i);``            ``return` `leaps;``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``Console.WriteLine(calculateLeaps(4));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``
1. Output:
`8`

2. The above solution can be improved by using Dynamic programming (Bottom-Up Approach)

```                              Leaps(3)
3/      2|         1\
Leaps(0)   Leaps(1)            Leaps(2)
/   |   \                   3/       2|     1\
Leaps(-3) Leaps(-2)  Leaps(-1)   Lepas(-1) Leaps(0) Leaps(1)```

## C++

 `// C++ program to count total number of ways``// to reach n-th stair with all jumps allowed` `#include ``using` `namespace` `std;` `int` `calculateLeaps(``int` `n, ``int` `dp[]){``    ``if``(n == 0){``        ``return` `1 ;``    ``}``else` `if``(n < 0){``        ``return` `0 ;``    ``}``    ` `    ``if``(dp[n] != 0 ){``       ``return` `dp[n] ;``    ``}` `    ``int` `count = 0;``    ``for``(``int` `i = 0 ; i < n ; i++ ){``        ``count += calculateLeaps(i, dp)  ;``    ``}``    ` `    ``dp[n] = count ;``    ``return` `count ;``    ` `}``int` `main() {``    ``int` `n = 4 ;``    ` `    ``int` `dp[n+1] = {0} ;``    ` `    ``cout<

## Python3

 `# Python program to count total number of ways``# to reach n-th stair with all jumps allowed``def` `calculateLeaps(n, dp):` `    ``if``(n ``=``=` `0``):``        ``return` `1``    ``elif``(n < ``0``):``        ``return` `0``    ` `    ``if``(dp[n] !``=` `0` `):``       ``return` `dp[n]` `    ``count ``=` `0``    ``for` `i ``in` `range``(n):``        ``count ``+``=` `calculateLeaps(i, dp)``    ` `    ``dp[n] ``=` `count``    ``return` `count``    ` `# driver code``n ``=` `4``dp ``=` `[``0``]``*``(n``+``1``)``print``(calculateLeaps(n,dp))` `# This code is contributed by shinjanpatra`

## Javascript

 ``

3. Let’s break this problem into small subproblems. The monkey has to step on the last step, the first N-1 steps are optional. The monkey can step on 0 steps before reaching the top step, which is the biggest leap to the top. Or it can decide to step on only once in between, which can be achieved in n-1 ways [ (N-1)C1 ]. And so on, it can step on only 2 steps before reaching the top in (N-1)C2 ways. Putting together..
F(N) = (N-1)C0 + (N-1)C1 + (N-1)C2 + … + (N-1)C(N-2) + (N-1)C(N-1)
Which is sum of binomial coefficient.
= 2^(n-1)

## C++

 `// C++ program to count total number of ways``// to reach n-th stair with all jumps allowed``#include ``using` `namespace` `std;` `     ``int` `calculateLeaps(``int` `n)``    ``{``        ``if` `(n == 0)``            ``return` `1;``        ``return` `(1 << (n - 1));``    ``}` `// Driver code``int` `main()``{``    ``int` `calculateLeaps(``int``);``    ``std::cout << calculateLeaps(4) << std::endl;``    ``return` `0;``}` `// This code is contributed by shivanisinghss2110.`

## Java

 `// Java program to count total number of ways``// to reach n-th stair with all jumps allowed``class` `GFG {``    ``static` `int` `calculateLeaps(``int` `n)``    ``{``        ``if` `(n == ``0``)``            ``return` `1``;``        ``return` `(``1` `<< (n - ``1``));``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``System.out.println(calculateLeaps(``4``));``    ``}``}``// This code is contributed by Anant Agarwal.`

## Python3

 `# python3 program to count``# total number of ways``# to reach n-th stair with``# all jumps allowed` `def` `calculateLeaps(n):``    ``if` `(n ``=``=` `0``):``        ``return` `1``;``    ``return` `(``1` `<< (n ``-` `1``));` `# Driver code``print``(calculateLeaps(``4``));` `# This code is contributed``# by mits`

## C#

 `// C# program to count total number of ways``// to reach n-th stair with all jumps allowed``using` `System;` `class` `GFG {` `    ``// Function to calculate leaps``    ``static` `int` `calculateLeaps(``int` `n)``    ``{``        ``if` `(n == 0)``            ``return` `1;``        ``return` `(1 << (n - 1));``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``Console.WriteLine(calculateLeaps(4));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output:

`8`

This article is contributed by Partha Pratim Mallik. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.