Given an integer N number of stairs, the task is count the number ways to reach the Nth stair by taking 1 or 2 step any number of times but taking a step of 3 exactly once.
Input: N = 4
Since a step of 3 has to be taken compulsorily and only once. So, there are only two possible ways: (1, 3) or (3, 1)
Input: N = 5
There are five possible ways to reach 5th stair: (2, 3), (3, 2), (1, 1, 3), (1, 3, 1), (3, 1, 1)
Approach: This problem can be solved using the concept of Permutations & Combinations. There is a constraint according to which 3 steps at a time have to be taken exactly once. The idea is to count the number of positions that are possible for 3 steps to be taken.
Now, the next task is to find the number of possible two-steps in the movement to Nth stair, which is (N – 3) / 2. In this way, all the possible ways will be covered if the count of two-step is incremented once at a time and for each step, all the possible combinations are calculated.
After Generalising the steps the possible combinations for each possible sequence will be
Ways = (Length of sequence)! * (Count of 2-step)! ------------------------------------------ (Count of 1-step)!
- Intialize the length of the sequence to (N – 2).
- Intialize the count of 1-steps to (N – 3).
- Intialize the count of 2-steps to 0
- Intialize the possible count of 2-steps to (N-3)/2.
- Run a loop until the possible count of 2-steps is equal to the actual count of 2-steps.
- Find the possible number of ways with the help of the current length of the sequence, count of 1-steps, count of 2-steps and by above-given formulae.
- Reduce the length of the sequence by 1.
- Increase the count of 2-steps by 1
Below is the implementation of the above approach.
- Time Complexity: As in the above approach, there is one loop to check the possible permutations for each sequence which can go uptil (N-3) which takes O(N) time and to find the possible combinations it will take O(N), Hence the Time Complexity will be O(N2).
- Space Complexity: As in the above approach, there is no extra space used, Hence the space complexity will be O(1).
Note: The time complexity can be improved up to O(N) by precomputing the factorials for every number till N.
- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach the Nth stair using any step from the given array
- Find the number of ways to reach Kth step in stair case
- Count ways to reach the n'th stair
- Count ways to reach end from start stone with at most K jumps at each step
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Count the minimum steps to reach 0 from the given integer N
- Count number of ways to jump to reach end
- Count number of ways to reach a given score in a Matrix
- Count of ways to traverse a Matrix and return to origin in K steps
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Find the number of stair steps
- Minimum step to reach one
- Reach A and B by multiplying them with K and K^2 at every step
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Number of steps to sort the array by changing order of three elements in each step
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Find the minimum number of steps to reach M from N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.