- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count ways to reach the n’th stair
There are n stairs. One person will go to 1'st to the n'th stair. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the n'th stairs.
Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to n'th stair, either from (n-1)'th stair or from (n-2)'th stair. So ways(n) = ways(n-1) + ways(n-2).
Input and Output
Input: The number of stairs, say 10 the maximum number of stairs that can be jumped in one step, say 2 Output: Enter number of stairs: 10 Enter max stair a person can climb: 2 Number of ways to reach: 89
Algorithm
stairClimpWays(stair, max)
Input − number of stairs, maximum stair jump in a single step.
Output − Number of possible ways to reach.
Begin define array count of size same as stair number count[0] := 1 count[0] := 1 for i := 2 to stair -1, do count[i] := 0 for j = 1 to i and j <= max; do count[i] := count[i] + count[i - j] done done return count[stair - 1] End
Example
#include<iostream> using namespace std; int stairClimbWays(int stair, int max) { int count[stair]; //fill the result stair using bottom up manner count[0] = 1; //when there are 0 or 1 stair, 1 way to climb count[1] = 1; for (int i=2; i<stair; i++) { //for stair 2 to higher count[i] = 0; for(int j=1; j<=max && j<=i; j++) count[i] += count[i-j]; } return count[stair-1]; } int countWays(int stair, int max) { //person can climb 1,2,...max stairs at a time return stairClimbWays(stair+1, max); } int main () { int stair, max; cout << "Enter number of stairs: "; cin >> stair; cout << "Enter max stair a person can climb: "; cin >> max; cout << "Number of ways to reach: " << countWays(stair, max); }
Output
Enter number of stairs: 10 Enter max stair a person can climb: 2 Number of ways to reach: 89
- Related Articles
- Count ways to reach the nth stair using step 1, 2 or 3 in C++
- Count number of ways to jump to reach end in C++
- Count number of ways to reach a given score in a game
- Count number of ways to reach destination in a Maze in C++
- Count number of ways to reach a given score in a Matrix in C++
- Count ways to reach a score using 1 and 2 with no consecutive 2s in C++
- C++ program to count number of operations needed to reach n by paying coins
- Count ways to divide circle using N non-intersecting chords in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Program to count number of ways we can throw n dices in Python
- If $m$ times the $m^{th}$ term of an AP is equal to $n$ times its $n^{th}$ term. find the $( m+n)^{th}$ term of the AP.
- Count possible ways to construct buildings
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- C++ code to count steps to reach final position by robot
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers

Advertisements