# Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step

Given a number **N** which denotes the number of stairs, the task is to reach the **N ^{th}** stair by taking 1, 2 steps any number of times and taking a step of 3 exactly once.

**Examples:**

Input:N = 4

Output:2

Explanation:

Since a step of 3 has to be taken compulsorily and only once,

There are only two possible ways: (1, 3) or (3, 1)

Input:N = 5

Output:5

Explanation:

Since a step of 3 has to be taken compulsorily and only once,

There are only 5 possible ways:

(1, 1, 3), (1, 3, 1), (3, 1, 1), (2, 3), (3, 2)

**Approach:** This problem can be solved using Dynamic Programming. To Reach N^{th} stair the person should be on (N – 1)^{th}, (N – 2)^{th} or (N – 3)^{th}. So, To reach N^{th} stair from the base Reach (N – 1)^{th} stair which includes exact only one step of 3, Reach (N – 2)^{th} step with the steps which inclues exactly one step of 3 and Reach the (N – 3)^{th} step without taking any step of 3.

So, The Recurrence Relation for this problem will be –

includes_3[i] = includes_3[i-1] + includes_3[i-2] + not_includes[i-3]

whereas, the Recurrence relation when the 3 number of steps at a time is not allowed is

not_includes[i] = not_includes[i – 1] + not_includes[i – 2]

Below is the implementation of the above approach.

## C++

`// C++ implmentation to find the number ` `// the number of ways to reach Nth stair ` `// by taking 1, 2 step at a time and ` `// 3 Steps at a time exactly once. ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find the number ` `// the number of ways to reach Nth stair ` `int` `number_of_ways(` `int` `n) ` `{ ` ` ` `// Array including number ` ` ` `// of ways that includes 3 ` ` ` `int` `includes_3[n + 1] = {}; ` ` ` ` ` `// Array including number of ` ` ` `// ways that doesn't includes 3 ` ` ` `int` `not_includes_3[n + 1] = {}; ` ` ` ` ` `// Intially to reach 3 stairs by ` ` ` `// taking 3 steps can be ` ` ` `// reached by 1 way ` ` ` `includes_3[3] = 1; ` ` ` ` ` `not_includes_3[1] = 1; ` ` ` `not_includes_3[2] = 2; ` ` ` `not_includes_3[3] = 3; ` ` ` ` ` `// Loop to find the number ` ` ` `// the number of ways to reach Nth stair ` ` ` `for` `(` `int` `i = 4; i <= n; i++) { ` ` ` `includes_3[i] ` ` ` `= includes_3[i - 1] + includes_3[i - 2] + not_includes_3[i - 3]; ` ` ` `not_includes_3[i] ` ` ` `= not_includes_3[i - 1] + not_includes_3[i - 2]; ` ` ` `} ` ` ` `return` `includes_3[n]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `cout << number_of_ways(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implmentation to find the number ` `// the number of ways to reach Nth stair ` `// by taking 1, 2 step at a time and ` `// 3 Steps at a time exactly once. ` `class` `GFG ` `{ ` ` ` `// Function to find the number ` `// the number of ways to reach Nth stair ` `static` `int` `number_of_ways(` `int` `n) ` `{ ` ` ` `// Array including number ` ` ` `// of ways that includes 3 ` ` ` `int` `[]includes_3 = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Array including number of ` ` ` `// ways that doesn't includes 3 ` ` ` `int` `[]not_includes_3 = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Intially to reach 3 stairs by ` ` ` `// taking 3 steps can be ` ` ` `// reached by 1 way ` ` ` `includes_3[` `3` `] = ` `1` `; ` ` ` ` ` `not_includes_3[` `1` `] = ` `1` `; ` ` ` `not_includes_3[` `2` `] = ` `2` `; ` ` ` `not_includes_3[` `3` `] = ` `3` `; ` ` ` ` ` `// Loop to find the number ` ` ` `// the number of ways to reach Nth stair ` ` ` `for` `(` `int` `i = ` `4` `; i <= n; i++) ` ` ` `{ ` ` ` `includes_3[i] ` ` ` `= includes_3[i - ` `1` `] + includes_3[i - ` `2` `] + ` ` ` `not_includes_3[i - ` `3` `]; ` ` ` `not_includes_3[i] ` ` ` `= not_includes_3[i - ` `1` `] + not_includes_3[i - ` `2` `]; ` ` ` `} ` ` ` `return` `includes_3[n]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` ` ` `System.out.print(number_of_ways(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implmentation to find the number ` `# the number of ways to reach Nth stair ` `# by taking 1, 2 step at a time and ` `# 3 Steps at a time exactly once. ` ` ` `# Function to find the number ` `# the number of ways to reach Nth stair ` `def` `number_of_ways(n): ` ` ` ` ` `# Array including number ` ` ` `# of ways that includes 3 ` ` ` `includes_3 ` `=` `[` `0` `]` `*` `(n ` `+` `1` `) ` ` ` ` ` `# Array including number of ` ` ` `# ways that doesn't includes 3 ` ` ` `not_includes_3 ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` ` ` `# Intially to reach 3 stairs by ` ` ` `# taking 3 steps can be ` ` ` `# reached by 1 way ` ` ` `includes_3[` `3` `] ` `=` `1` ` ` ` ` `not_includes_3[` `1` `] ` `=` `1` ` ` `not_includes_3[` `2` `] ` `=` `2` ` ` `not_includes_3[` `3` `] ` `=` `3` ` ` ` ` `# Loop to find the number ` ` ` `# the number of ways to reach Nth stair ` ` ` `for` `i ` `in` `range` `(` `4` `, n ` `+` `1` `): ` ` ` `includes_3[i] ` `=` `includes_3[i ` `-` `1` `] ` `+` `\ ` ` ` `includes_3[i ` `-` `2` `] ` `+` `\ ` ` ` `not_includes_3[i ` `-` `3` `] ` ` ` `not_includes_3[i] ` `=` `not_includes_3[i ` `-` `1` `] ` `+` `\ ` ` ` `not_includes_3[i ` `-` `2` `] ` ` ` `return` `includes_3[n] ` ` ` `# Driver Code ` `n ` `=` `7` ` ` `print` `(number_of_ways(n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implmentation to find the number ` `// the number of ways to reach Nth stair ` `// by taking 1, 2 step at a time and ` `// 3 Steps at a time exactly once. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the number ` `// the number of ways to reach Nth stair ` `static` `int` `number_of_ways(` `int` `n) ` `{ ` ` ` `// Array including number ` ` ` `// of ways that includes 3 ` ` ` `int` `[]includes_3 = ` `new` `int` `[n + 1]; ` ` ` ` ` `// Array including number of ` ` ` `// ways that doesn't includes 3 ` ` ` `int` `[]not_includes_3 = ` `new` `int` `[n + 1]; ` ` ` ` ` `// Intially to reach 3 stairs by ` ` ` `// taking 3 steps can be ` ` ` `// reached by 1 way ` ` ` `includes_3[3] = 1; ` ` ` ` ` `not_includes_3[1] = 1; ` ` ` `not_includes_3[2] = 2; ` ` ` `not_includes_3[3] = 3; ` ` ` ` ` `// Loop to find the number ` ` ` `// the number of ways to reach Nth stair ` ` ` `for` `(` `int` `i = 4; i <= n; i++) ` ` ` `{ ` ` ` `includes_3[i] ` ` ` `= includes_3[i - 1] + includes_3[i - 2] + ` ` ` `not_includes_3[i - 3]; ` ` ` `not_includes_3[i] ` ` ` `= not_includes_3[i - 1] + not_includes_3[i - 2]; ` ` ` `} ` ` ` `return` `includes_3[n]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `Console.Write(number_of_ways(n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

20

**Similar Article:** Count ways to reach Nth Stair using 1, 2 or 3 steps at a time

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach end from start stone with at most K jumps at each step
- Number of ways to reach Nth floor by taking at-most K leaps
- Find the number of ways to reach Kth step in stair case
- Minimum cost to reach the top of the floor by climbing stairs
- Count ways to reach the n'th stair
- Count number of ways to jump to reach end
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to reach destination in a Maze
- Count number of ways to reach a given score in a Matrix
- Count number of ways to reach a given score in a game
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Minimum steps to reach a destination
- Minimum steps to reach target by a Knight | Set 2
- Find minimum steps required to reach the end of a matrix | Set - 1

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.