# Count of ways to travel a cyclic path in N steps in a Triangular Pyramid

• Difficulty Level : Hard
• Last Updated : 26 Aug, 2021

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:
Explanation:
In 1 step, it is impossible to be again at position O.
Input: N = 2
Output:
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:
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

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```
1. 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` `fun(``int` `n)``{``    ``int` `sum=0;``    ``for``(``int` `i=1;i

## 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`

## Javascript

 ``

Output:

```6
21```

Time Complexity: O(N).
Auxiliary Space Complexity: O(1)
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..

My Personal Notes arrow_drop_up