Related Articles
Count ways to reach the nth stair using step 1, 2 or 3
• Difficulty Level : Easy
• Last Updated : 09 Nov, 2020

A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.

Examples:

```Input : 4
Output : 7
Explantion:
Below are the four ways
1 step + 1 step + 1 step + 1 step
1 step + 2 step + 1 step
2 step + 1 step + 1 step
1 step + 1 step + 2 step
2 step + 2 step
3 step + 1 step
1 step + 3 step

Input : 3
Output : 4
Explantion:
Below are the four ways
1 step + 1 step + 1 step
1 step + 2 step
2 step + 1 step
3 step
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

There are two methods to solve this problem:

1. Recursive Method
2. Dynamic Programming

Method 1: Recursive.
There are n stairs, and a person is allowed to jump next stair, skip one stair or skip two stairs. So there are n stairs. So if a person is standing at i-th stair, the person can move to i+1, i+2, i+3-th stair. A recursive function can be formed where at current index i the function is recursively called for i+1, i+2 and i+3 th stair.
There is another way of forming the recursive function. To reach a stair i, a person has to jump either from i-1, i-2 or i-3 th stair or i is the starting stair.

Algorithm:

1. Create a recursive function (count(int n)) which takes only one parameter.
2. Check the base cases. If the value of n is less than 0 then return 0, and if the value of n is equal to zero then return 1 as it is the starting stair.
3. Call the function recursively with values n-1, n-2 and n-3 and sum up the values that are returned, i.e. sum = count(n-1) + count(n-2) + count(n-3)
4. Return the value of the sum.

## C++

 `// C++ Program to find n-th stair using step size ` `// 1 or 2 or 3. ` `#include ` `using` `namespace` `std; ` ` `  `class` `GFG { ` ` `  `    ``// Returns count of ways to reach n-th stair ` `    ``// using 1 or 2 or 3 steps. ` `public``: ` `    ``int` `findStep(``int` `n) ` `    ``{ ` `        ``if` `(n == 1 || n == 0) ` `            ``return` `1; ` `        ``else` `if` `(n == 2) ` `            ``return` `2; ` ` `  `        ``else` `            ``return` `findStep(n - 3) + findStep(n - 2) ` `                                   ``+ findStep(n - 1); ` `    ``} ` `}; ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``GFG g; ` `    ``int` `n = 4; ` `    ``cout << g.findStep(n); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by SoM15242 `

## C

 `// Program to find n-th stair using step size ` `// 1 or 2 or 3. ` `#include ` ` `  `// Returns count of ways to reach n-th stair ` `// using 1 or 2 or 3 steps. ` `int` `findStep(``int` `n) ` `{ ` `    ``if` `(n == 1 || n == 0) ` `        ``return` `1; ` `    ``else` `if` `(n == 2) ` `        ``return` `2; ` ` `  `    ``else` `        ``return` `findStep(n - 3) + findStep(n - 2) + findStep(n - 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``printf``(``"%d\n"``, findStep(n)); ` `    ``return` `0; ` `} `

## Java

 `// Program to find n-th stair ` `// using step size 1 or 2 or 3. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GfG { ` ` `  `    ``// Returns count of ways to reach ` `    ``// n-th stair using 1 or 2 or 3 steps. ` `    ``public` `static` `int` `findStep(``int` `n) ` `    ``{ ` `        ``if` `(n == ``1` `|| n == ``0``) ` `            ``return` `1``; ` `        ``else` `if` `(n == ``2``) ` `            ``return` `2``; ` ` `  `        ``else` `            ``return` `findStep(n - ``3``) + findStep(n - ``2``) + findStep(n - ``1``); ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `main(String argc[]) ` `    ``{ ` `        ``int` `n = ``4``; ` `        ``System.out.println(findStep(n)); ` `    ``} ` `} ` ` `  `/* This code is contributed by Sagar Shukla */`

## Python

 `# Python program to find n-th stair   ` `# using step size 1 or 2 or 3. ` ` `  `# Returns count of ways to reach n-th  ` `# stair using 1 or 2 or 3 steps. ` `def` `findStep( n) : ` `    ``if` `(n ``=``=` `1` `or` `n ``=``=` `0``) : ` `        ``return` `1` `    ``elif` `(n ``=``=` `2``) : ` `        ``return` `2` `     `  `    ``else` `: ` `        ``return` `findStep(n ``-` `3``) ``+` `findStep(n ``-` `2``) ``+` `findStep(n ``-` `1``)  ` ` `  ` `  `# Driver code ` `n ``=` `4` `print``(findStep(n)) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// Program to find n-th stair ` `// using step size 1 or 2 or 3. ` `using` `System; ` ` `  `public` `class` `GfG { ` ` `  `    ``// Returns count of ways to reach ` `    ``// n-th stair using 1 or 2 or 3 steps. ` `    ``public` `static` `int` `findStep(``int` `n) ` `    ``{ ` `        ``if` `(n == 1 || n == 0) ` `            ``return` `1; ` `        ``else` `if` `(n == 2) ` `            ``return` `2; ` ` `  `        ``else` `            ``return` `findStep(n - 3) + findStep(n - 2) + findStep(n - 1); ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 4; ` `        ``Console.WriteLine(findStep(n)); ` `    ``} ` `} ` ` `  `/* This code is contributed by vt_m */`

## PHP

 ` `

Output :

```7
```

Complexity Analysis:

• Time Complexity: O(3n).
The time complexity of the above solution is exponential, a close upper bound will be O(3n). From each state, 3 recursive function are called. So the upperbound for n states is O(3n).
• Space Complexity:O(1).
As no extra space is required.

Note: The Time Complexity of the program can be optimized using Dynamic Programming.

Method 2: Dynamic Programming.

The idea is similar, but it can be observed that there are n states but the recursive function is called 3 ^ n times. That means that some states are called repeatedly. So the idea is to store the value of states. This can be done in two ways.

• Top-Down Approach: The first way is to keep the recursive structure intact and just store the value in a HashMap and whenever the function is called again return the value store without computing ().
• Bottom-Up Approach: The second way is to take an extra space of size n and start computing values of states from 1, 2 .. to n, i.e. compute values of i, i+1, i+2 and then use them to calculate the value of i+3.

Algorithm:

1. Create an array of size n + 1 and initialize the first 3 variables with 1, 1, 2. The base cases.
2. Run a loop from 3 to n.
3. For each index i, computer value of ith position as dp[i] = dp[i-1] + dp[i-2] + dp[i-3].
4. Print the value of dp[n], as the Count of the number of ways to reach n th step.

## C++

 `// A C++ program to count number of ways ` `// to reach n't stair when ` `#include ` `using` `namespace` `std; ` ` `  `// A recursive function used by countWays ` `int` `countWays(``int` `n) ` `{ ` `    ``int` `res[n + 1]; ` `    ``res = 1; ` `    ``res = 1; ` `    ``res = 2; ` `    ``for` `(``int` `i = 3; i <= n; i++) ` `        ``res[i] = res[i - 1] + res[i - 2] ` `                ``+ res[i - 3]; ` ` `  `    ``return` `res[n]; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``cout << countWays(n); ` `    ``return` `0; ` `} ` `//This code is contributed by shubhamsingh10 `

## C

 `// A C program to count number of ways ` `// to reach n't stair when ` `#include ` ` `  `// A recursive function used by countWays ` `int` `countWays(``int` `n) ` `{ ` `    ``int` `res[n + 1]; ` `    ``res = 1; ` `    ``res = 1; ` `    ``res = 2; ` `    ``for` `(``int` `i = 3; i <= n; i++) ` `        ``res[i] = res[i - 1] + res[i - 2] ` `                 ``+ res[i - 3]; ` ` `  `    ``return` `res[n]; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``printf``(``"%d"``, countWays(n)); ` `    ``return` `0; ` `} `

## Java

 `// Program to find n-th stair ` `// using step size 1 or 2 or 3. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GfG { ` ` `  `    ``// A recursive function used by countWays ` `    ``public` `static` `int` `countWays(``int` `n) ` `    ``{ ` `        ``int``[] res = ``new` `int``[n + ``1``]; ` `        ``res[``0``] = ``1``; ` `        ``res[``1``] = ``1``; ` `        ``res[``2``] = ``2``; ` ` `  `        ``for` `(``int` `i = ``3``; i <= n; i++) ` `            ``res[i] = res[i - ``1``] + res[i - ``2``] ` `                     ``+ res[i - ``3``]; ` ` `  `        ``return` `res[n]; ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `main(String argc[]) ` `    ``{ ` `        ``int` `n = ``4``; ` `        ``System.out.println(countWays(n)); ` `    ``} ` `} ` ` `  `/* This code is contributed by Sagar Shukla */`

## Python

 `# Python program to find n-th stair   ` `# using step size 1 or 2 or 3. ` ` `  `# A recursive function used by countWays ` `def` `countWays(n) : ` `    ``res ``=` `[``0``] ``*` `(n ``+` `2``) ` `    ``res[``0``] ``=` `1` `    ``res[``1``] ``=` `1` `    ``res[``2``] ``=` `2` `     `  `    ``for` `i ``in` `range``(``3``, n ``+` `1``) : ` `        ``res[i] ``=` `res[i ``-` `1``] ``+` `res[i ``-` `2``] ``+` `res[i ``-` `3``] ` `     `  `    ``return` `res[n] ` ` `  `# Driver code ` `n ``=` `4` `print``(countWays(n)) ` ` `  ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// Program to find n-th stair ` `// using step size 1 or 2 or 3. ` `using` `System; ` ` `  `public` `class` `GfG { ` ` `  `    ``// A recursive function used by countWays ` `    ``public` `static` `int` `countWays(``int` `n) ` `    ``{ ` `        ``int``[] res = ``new` `int``[n + 2]; ` `        ``res = 1; ` `        ``res = 1; ` `        ``res = 2; ` ` `  `        ``for` `(``int` `i = 3; i <= n; i++) ` `            ``res[i] = res[i - 1] + res[i - 2] ` `                     ``+ res[i - 3]; ` ` `  `        ``return` `res[n]; ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 4; ` `        ``Console.WriteLine(countWays(n)); ` `    ``} ` `} ` ` `  `/* This code is contributed by vt_m */`

## PHP

 ` `

Output :

```7
```
• Working:

```1 -> 1 -> 1 -> 1
1 -> 1 -> 2
1 -> 2 -> 1
1 -> 3
2 -> 1 -> 1
2 -> 2
3 -> 1

So Total ways: 7```

Complexity Analysis:

• Time Complexity: O(n).
Only one traversal of the array is needed. So Time Complexity is O(n).
• Space Complexity: O(n).
To store the values in a DP, n extra space is needed.

Other Related Articles
http://www.geeksforgeeks.org/count-ways-reach-nth-stair/

This article is contributed by Prakhar Agrawal. 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.