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 different ways it can reach to 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.**

- In all possible solutions, a step is either stepped on by the monkey or can be skipped. So using fundamental counting principle, first step has 2 ways to take part, and for each of this, 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. - 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 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 alowed`

`#include <iostream>`

`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 alowed`

`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 alowed`

`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 alowed`

`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

`<?php`

`// PHP program to count total`

`// number of ways to reach`

`// n-th stair with all`

`// jumps allowed`

`// function return the`

`// number of ways`

`function`

`calculateLeaps(`

`$n`

`)`

`{`

`if`

`(`

`$n`

`== 0 ||`

`$n`

`== 1)`

`{`

`return`

`1;`

`}`

`else`

`{`

`$leaps`

`= 0;`

`for`

`(`

`$i`

`= 0;`

`$i`

`<`

`$n`

`;`

`$i`

`++)`

`$leaps`

`+= calculateLeaps(`

`$i`

`);`

`return`

`$leaps`

`;`

`}`

`}`

`// Driver Code`

`echo`

`calculateLeaps(4),`

`"\n"`

`;`

`// This code is contributed by ajit`

`?>`

**Output:**

8

The above solution can be improved by using Dynamic programming

- 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 step before reaching the top step, which is the biggest leap to top. Or it can decide to step on only once in between, which can be achieved in n-1 ways [
^{(N-1)}C_{1}]. And so on, it can step on only 2 steps before reaching top in^{(N-1)}C_{2}ways. Putting together..F(N) =

^{(N-1)}C_{0}+^{(N-1)}C_{1}+^{(N-1)}C_{2}+ ... +^{(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 alowed ` `#include <iostream> ` ` ` `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; ` `} ` |

## Java

`// Java program to count total number of ways ` `// to reach n-th stair with all jumps alowed ` `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 alowed ` ` ` `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 alowed ` `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

`<?php ` `// PHP program to count total ` `// number of ways to reach n-th ` `// stair with all jumps alowed ` ` ` `// Function to calculate leaps ` `function` `calculateLeaps(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 0) ` ` ` `return` `1; ` ` ` `return` `(1 << (` `$n` `- 1)); ` `} ` ` ` `// Driver code ` `echo` `calculateLeaps(4); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

**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 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.