Related Articles
Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
• Difficulty Level : Medium
• Last Updated : 15 Jun, 2020

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. Examples:

Input: N = 1
Output: 0
Explanation:
In 1 step, it is impossible to be again at position O.

Input: N = 2
Output: 3
Explanation:
The three ways to reach back to O in two steps are:
O->A->O
O->B->O
O->C->O

Input: N = 3
Output: 6
Explanation:
The 6 ways to reach back to O in three steps are:
O->A->B->O
O->A->C->O
O->B->A->O
O->B->C->O
O->C->A->O
O->C->B->O

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use the concept of Dynamic programming.

1. A table T[][] is created where the row represents the number of ways and the column represents the position.
2. 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.
3. 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.
4. 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
```
5. 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

## C++

 `// C++ program for Dynamic``// Programming implementation of``// Number of Path in a Triangular``// pyramid`` ` `#include ``using` `namespace` `std;`` ` `// Function to return the number of``// ways we can reach back to the``// initial position O``int` `count(``int` `n)``{``    ``// If n is 0 then there is``    ``// 1 solution``    ``if` `(n == 0)``        ``return` `1;`` ` `    ``// If n is equal to 1``    ``// then we can't reach at position O``    ``if` `(n == 1)``        ``return` `0;`` ` `    ``int` `dp[n + 1];`` ` `    ``// Initial Conditions`` ` `    ``// Represents position O``    ``dp = 1;`` ` `    ``// Represents position A``    ``dp = 0;`` ` `    ``// Represents position B``    ``dp = 0;`` ` `    ``// Represents position C``    ``dp = 0;`` ` `    ``// Filling the table``    ``for` `(``int` `i = 1; i <= n; i++) {`` ` `        ``// The number of ways to reach``        ``// a particular position (say X)``        ``// at the i'th step is equivalent``        ``// to the sum of the number``        ``// of ways the person is not at``        ``// position X in the last step.`` ` `        ``int` `countPositionO``            ``= dp[i - 1] + dp[i - 1]``              ``+ dp[i - 1];`` ` `        ``int` `countPositionA``            ``= dp[i - 1] + dp[i - 1]``              ``+ dp[i - 1];`` ` `        ``int` `countPositionB``            ``= dp[i - 1] + dp[i - 1]``              ``+ dp[i - 1];`` ` `        ``int` `countPositionC``            ``= dp[i - 1] + dp[i - 1]``              ``+ dp[i - 1];`` ` `        ``dp[i] = countPositionO;``        ``dp[i] = countPositionA;``        ``dp[i] = countPositionB;``        ``dp[i] = countPositionC;``    ``}`` ` `    ``return` `dp[n];``}`` ` `// Driver code``int` `main()``{`` ` `    ``int` `n = 3;``    ``cout << count(n) << endl;`` ` `    ``n = 4;``    ``cout << count(n) << endl;`` ` `    ``return` `0;``}`

## Java

 `// Java program for dynamic programming``// implementation of number of path in ``// a triangular pyramid``class` `GFG{`` ` `// Function to return the number of``// ways we can reach back to the``// initial position O``static` `int` `count(``int` `n)``{``     ` `    ``// If n is 0 then there is``    ``// 1 solution``    ``if` `(n == ``0``)``        ``return` `1``;`` ` `    ``// If n is equal to 1 then we``    ``// can't reach at position O``    ``if` `(n == ``1``)``        ``return` `0``;`` ` `    ``int` `[][]dp = ``new` `int``[``4``][n + ``1``];`` ` `    ``// Initial Conditions`` ` `    ``// Represents position O``    ``dp[``0``][``0``] = ``1``;`` ` `    ``// Represents position A``    ``dp[``1``][``0``] = ``0``;`` ` `    ``// Represents position B``    ``dp[``2``][``0``] = ``0``;`` ` `    ``// Represents position C``    ``dp[``3``][``0``] = ``0``;`` ` `    ``// Filling the table``    ``for``(``int` `i = ``1``; i <= n; i++)``    ``{``        ` `       ``// The number of ways to reach``       ``// a particular position (say X)``       ``// at the i'th step is equivalent``       ``// to the sum of the number``       ``// of ways the person is not at``       ``// position X in the last step.``       ``int` `countPositionO = dp[``1``][i - ``1``] + ``                            ``dp[``2``][i - ``1``] + ``                            ``dp[``3``][i - ``1``];``       ``int` `countPositionA = dp[``0``][i - ``1``] + ``                            ``dp[``2``][i - ``1``] + ``                            ``dp[``3``][i - ``1``];``       ``int` `countPositionB = dp[``0``][i - ``1``] + ``                            ``dp[``1``][i - ``1``] + ``                            ``dp[``3``][i - ``1``];``       ``int` `countPositionC = dp[``0``][i - ``1``] + ``                            ``dp[``1``][i - ``1``] + ``                            ``dp[``2``][i - ``1``];``        ` `       ``dp[``0``][i] = countPositionO;``       ``dp[``1``][i] = countPositionA;``       ``dp[``2``][i] = countPositionB;``       ``dp[``3``][i] = countPositionC;``    ``}``    ``return` `dp[``0``][n];``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``3``;``    ``System.out.print(count(n) + ``"\n"``);`` ` `    ``n = ``4``;``    ``System.out.print(count(n) + ``"\n"``);``}``}`` ` `// This code is contributed by sapnasingh4991`

## Python3

 `# Python3 program for Dynamic``# Programming implementation of``# Number of Path in a Triangular``# pyramid`` ` `# Function to return the number of``# ways we can reach back to the``# initial position O``def` `count(n):`` ` `    ``# If n is 0 then there is``    ``# 1 solution``    ``if` `(n ``=``=` `0``):``        ``return` `1`` ` `    ``# If n is equal to 1``    ``# then we can't reach at position O``    ``if` `(n ``=``=` `1``):``        ``return` `0`` ` `    ``dp ``=` `[[``0` `for` `i ``in` `range``(n ``+` `1``)] ``             ``for` `j ``in` `range``(``4``)]`` ` `    ``# Initial Conditions`` ` `    ``# Represents position O``    ``dp[``0``][``0``] ``=` `1`` ` `    ``# Represents position A``    ``dp[``1``][``0``] ``=` `0`` ` `    ``# Represents position B``    ``dp[``2``][``0``] ``=` `0`` ` `    ``# Represents position C``    ``dp[``3``][``0``] ``=` `0`` ` `    ``# Filling the table``    ``for` `i ``in` `range``(``1``, n ``+` `1``):`` ` `        ``# The number of ways to reach``        ``# a particular position (say X)``        ``# at the i'th step is equivalent``        ``# to the sum of the number``        ``# of ways the person is not at``        ``# position X in the last step.`` ` `        ``countPositionO ``=` `(dp[``1``][i ``-` `1``] ``+` `                          ``dp[``2``][i ``-` `1``] ``+` `                          ``dp[``3``][i ``-` `1``])`` ` `        ``countPositionA ``=` `(dp[``0``][i ``-` `1``] ``+` `                          ``dp[``2``][i ``-` `1``] ``+` `                          ``dp[``3``][i ``-` `1``])`` ` `        ``countPositionB ``=` `(dp[``0``][i ``-` `1``] ``+` `                          ``dp[``1``][i ``-` `1``] ``+` `                          ``dp[``3``][i ``-` `1``])`` ` `        ``countPositionC ``=` `(dp[``0``][i ``-` `1``] ``+` `                          ``dp[``1``][i ``-` `1``] ``+` `                          ``dp[``2``][i ``-` `1``])`` ` `        ``dp[``0``][i] ``=` `countPositionO``        ``dp[``1``][i] ``=` `countPositionA``        ``dp[``2``][i] ``=` `countPositionB``        ``dp[``3``][i] ``=` `countPositionC``     ` `    ``return` `dp[``0``][n]`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:`` ` `    ``n ``=` `3``    ``print``(count(n))`` ` `    ``n ``=` `4``    ``print``(count(n))`` ` `# This code is contributed by ChitraNayal`

## C#

 `// C# program for dynamic programming``// implementation of number of path in ``// a triangular pyramid``using` `System;`` ` `class` `GFG{`` ` `// Function to return the number ``// of ways we can reach back to ``// the initial position O``static` `int` `count(``int` `n)``{``     ` `    ``// If n is 0 then there is``    ``// 1 solution``    ``if` `(n == 0)``        ``return` `1;`` ` `    ``// If n is equal to 1 then we``    ``// can't reach at position O``    ``if` `(n == 1)``        ``return` `0;`` ` `    ``int` `[,]dp = ``new` `int``[4, n + 1];`` ` `    ``// Initial Conditions`` ` `    ``// Represents position O``    ``dp[0, 0] = 1;`` ` `    ``// Represents position A``    ``dp[1, 0] = 0;`` ` `    ``// Represents position B``    ``dp[2, 0] = 0;`` ` `    ``// Represents position C``    ``dp[3, 0] = 0;`` ` `    ``// Filling the table``    ``for``(``int` `i = 1; i <= n; i++)``    ``{``         ` `       ``// The number of ways to reach``       ``// a particular position (say X)``       ``// at the i'th step is equivalent``       ``// to the sum of the number``       ``// of ways the person is not at``       ``// position X in the last step.``       ``int` `countPositionO = dp[1, i - 1] + ``                            ``dp[2, i - 1] + ``                            ``dp[3, i - 1];``       ``int` `countPositionA = dp[0, i - 1] + ``                            ``dp[2, i - 1] + ``                            ``dp[3, i - 1];``       ``int` `countPositionB = dp[0, i - 1] +``                            ``dp[1, i - 1] + ``                            ``dp[3, i - 1];``       ``int` `countPositionC = dp[0, i - 1] + ``                            ``dp[1, i - 1] +``                            ``dp[2, i - 1];``        ` `       ``dp[0, i] = countPositionO;``       ``dp[1, i] = countPositionA;``       ``dp[2, i] = countPositionB;``       ``dp[3, i] = countPositionC;``    ``}``    ``return` `dp[0, n];``}`` ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `n = 3;``    ``Console.Write(count(n) + ``"\n"``);`` ` `    ``n = 4;``    ``Console.Write(count(n) + ``"\n"``);``}``}`` ` `// This code is contributed by sapnasingh4991`
Output:
```6
21
```

Time Complexity: O(N).
Auxiliary Space Complexity: O(4*N)

Note:

• 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up