Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices.
Input: N = 1
In 1 step, it is impossible to be again at position O.
Input: N = 2
The three ways to reach back to O in two steps are:
Input: N = 3
The 6 ways to reach back to O in three steps are:
Approach: The idea is to use the concept of Dynamic programming.
- A table T is created where the row represents the number of ways and the column represents the position.
- In order to fill the table, one observation needs to be made. That is, we can go back to the position O if we are not at O in the previous step.
- Therefore, the number of ways to reach the origin O in the current step is equal to the sum of the number of ways the person is not at the origin O in the previous steps.
- Lets understand how the table is filled for N = 3:
0 1 2 3 O 1 0 3 6 A 0 1 2 7 B 0 1 2 7 C 0 1 2 7
- The base case for this table is when N = 1. We can reach the origin in 1 step from all the positions except O.
Below is the implementation of the above approach:
Using Tabulation Approach
Time Complexity: O(N).
Auxiliary Space Complexity: O(4*N)
- This program works more efficiently to find the number of ways in constant time after preprocessing for multiple queries if we fill the table for the largest number among the set of queries.
- This program can also be made more space-efficient for a single query. The idea is that since we need just previous step values to compute the present step values, so by just taking 4 variables to store the previous step values, we can solve the given problem in constant space.
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
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Count minimum steps to get the given desired array
- Count of 1's in any path in a Binary Tree
- Count number of ways to get Odd Sum
- Count of different ways to express N as the sum of 1, 3 and 4
- Count possible ways to construct buildings
- Count ways to reach the n'th stair
- Count ways to reach the nth stair using step 1, 2 or 3
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Count number of ways to jump to reach end
- Count ways to increase LCS length of two strings by one
- Count number of ways to cover a distance | Set 2
- Count the number of ways to traverse a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : sapnasingh4991