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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Count of ways to traverse a Matrix and return to origin in K steps
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count minimum steps to get the given desired array
- Count of 1's in any path in a Binary Tree
- Menu-Driven program to implement Travel Agency
- Count of different ways to express N as the sum of 1, 3 and 4
- Count number of ways to get Odd Sum
- Count possible ways to construct buildings
- Count ways to reach the n'th stair
- Count number of ways to arrange first N numbers
- Count number of ways to jump to reach end
- Count ways to increase LCS length of two strings by one
- Count ways to reach the nth stair using step 1, 2 or 3
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to cover a distance | Set 2
- Count ways to build street under given constraints
- 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.