• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
April 16, 2024 |170 Views
Count ways to reach the nth stair using step 1, 2 or 3
Description
Discussion

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. 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. Count ways to reach the nth stair using step 1, 2 or 3 : https://www.geeksforgeeks.org/count-ways-reach-nth-stair-using-step-1-2-3/