Skip to content
Related Articles

Related Articles

Improve Article

Count ways to reach the Nth station

  • Last Updated : 10 Jun, 2021

Given N stations and three trains A, B, and C such that train A stops at every station, train B stops at every second station, and train C stops at every third station, the task is to find the number of ways to reach the Nth station can be reached from station 1.

Examples:

Input: N = 4
Output: 3
Explanation:
Below are the ways of reaching station 4 from station 1 as follows:

  1. Take train A at station 1 and continue to station 4 using only train A as (A → A → A → A).
  2. Take train B at station 1, stop at station 3 and take train A from station 3 to 4 as (B→ . → A).
  3. Take train C from station 1 and stop at station 4(C→ .)

Therefore, the total number of ways to reach station 4 from station 1 is 3.

Input: N = 15
Output: 338



Approach: The given problem can be solved using the following observations:

  • Train A can be used to reach station X only if train A reaches X – 1.
  • Train B can be used to reach station X only if train B reaches X – 2.
  • Train C can be used to reach station X only if train C reaches X – 3.

Therefore, from the above observations, the idea is to use the concept of Dynamic Programming. Follow the steps given below to solve the problem:

  • Initialize a 2D array DP such that:
    • DP[i][1] stores the number of ways to reach station i using train A.
    • DP[i][2] stores the number of ways to reach station i using train B.
    • DP[i][3] stores the number of ways to reach station i using train C.
    • DP[i][4] stores the number of ways to reach station i using trains A, B, or C.
  • There is only one way of reaching station 1. Therefore, update the value of DP[1][1], DP[1][2], DP[1][3], DP[1][4] as 1.
  • Using the above observations, update the value of each state by iterating the loop as follows:
    • DP[i][1] = DP[i-1][4]
    • DP[i][2] = DP[i-2][4]
    • DP[i][3] = DP[i-3][4]
    • DP[i][4] = DP[i][1] + DP[i][2] + DP[i][3]
  • After completing the above steps, print the value of DP[N][4] as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of ways
// to reach Nth station
int numberOfWays(int N)
{
    // Declares the DP[] array
    int DP[N + 1][5];
 
    // Initialize dp[][] array
    memset(DP, 0, sizeof(DP));
 
    // Only 1 way to reach station 1
    DP[1][1] = 1;
    DP[1][2] = 1;
    DP[1][3] = 1;
    DP[1][4] = 1;
 
    // Find the remaining states from
    // the 2nd station
    for (int i = 2; i <= N; i++) {
 
        // If the train A is present
        // at station i - 1
        if (i - 1 > 0 && DP[i - 1][1] > 0)
            DP[i][1] = DP[i - 1][4];
 
        // If the train B is present
        // at station i-2
        if (i - 2 > 0 && DP[i - 2][2] > 0)
            DP[i][2] = DP[i - 2][4];
 
        // If train C is present at
        // station i-3
        if (i - 3 > 0 && DP[i - 3][3] > 0)
            DP[i][3] = DP[i - 3][4];
 
        // The total number of ways to
        // reach station i
        DP[i][4] = (DP[i][1] + DP[i][2]
                    + DP[i][3]);
    }
 
    // Return the total count of ways
    return DP[N][4];
}
 
// Driver Code
int main()
{
    int N = 15;
    cout << numberOfWays(N);
 
    return 0;
}

Java




// Java program for the above approach
 
class GFG {
 
    // Function to find the number of ways
    // to reach Nth station
    static int numberOfWays(int N)
    {
 
        // Declares the DP[] array
        int[][] DP = new int[N + 1][5];
 
        // Initialize dp[][] array
        for (int i = 0; i < N + 1; i++) {
            for (int j = 0; j < 5; j++) {
                DP[i][j] = 0;
            }
        }
 
        // Only 1 way to reach station 1
        DP[1][1] = 1;
        DP[1][2] = 1;
        DP[1][3] = 1;
        DP[1][4] = 1;
 
        // Find the remaining states from
        // the 2nd station
        for (int i = 2; i <= N; i++) {
 
            // If the train A is present
            // at station i - 1
            if (i - 1 > 0 && DP[i - 1][1] > 0)
                DP[i][1] = DP[i - 1][4];
 
            // If the train B is present
            // at station i-2
            if (i - 2 > 0 && DP[i - 2][2] > 0)
                DP[i][2] = DP[i - 2][4];
 
            // If train C is present at
            // station i-3
            if (i - 3 > 0 && DP[i - 3][3] > 0)
                DP[i][3] = DP[i - 3][4];
 
            // The total number of ways to
            // reach station i
            DP[i][4] = (DP[i][1] + DP[i][2] + DP[i][3]);
        }
 
        // Return the total count of ways
        return DP[N][4];
    }
 
    // Driver Code
    static public void main(String[] args)
    {
        int N = 15;
        System.out.println(numberOfWays(N));
    }
}
 
// This code is contributed by ukasp.

Python3




# Python3 program for the above approach
 
# Function to find the number of ways
# to reach Nth station
def numberOfWays(N):
     
    # Declares the DP[] array
    DP = [[0 for i in range(5)]
             for i in range(N + 1)]
 
    # Initialize dp[][] array
    # memset(DP, 0, sizeof(DP))
     
    # Only 1 way to reach station 1
    DP[1][1] = 1
    DP[1][2] = 1
    DP[1][3] = 1
    DP[1][4] = 1
 
    # Find the remaining states from
    # the 2nd station
    for i in range(2, N + 1):
         
        # If the train A is present
        # at station i - 1
        if (i - 1 > 0 and DP[i - 1][1] > 0):
            DP[i][1] = DP[i - 1][4]
 
        # If the train B is present
        # at station i-2
        if (i - 2 > 0 and DP[i - 2][2] > 0):
            DP[i][2] = DP[i - 2][4]
 
        # If train C is present at
        # station i-3
        if (i - 3 > 0 and DP[i - 3][3] > 0):
            DP[i][3] = DP[i - 3][4]
 
        # The total number of ways to
        # reach station i
        DP[i][4] = (DP[i][1] + DP[i][2] + DP[i][3])
 
    # Return the total count of ways
    return DP[N][4]
 
# Driver Code
if __name__ == '__main__':
     
    N = 15
     
    print(numberOfWays(N))
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the number of ways
// to reach Nth station
static int numberOfWays(int N)
{
     
    // Declares the DP[] array
    int[,] DP = new int[N + 1, 5];
 
    // Initialize dp[][] array
    for(int i = 0; i < N + 1; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            DP[i, j] = 0;
        }
    }
 
    // Only 1 way to reach station 1
    DP[1, 1] = 1;
    DP[1, 2] = 1;
    DP[1, 3] = 1;
    DP[1, 4] = 1;
 
    // Find the remaining states from
    // the 2nd station
    for(int i = 2; i <= N; i++)
    {
         
        // If the train A is present
        // at station i - 1
        if (i - 1 > 0 && DP[i - 1, 1] > 0)
            DP[i, 1] = DP[i - 1, 4];
 
        // If the train B is present
        // at station i-2
        if (i - 2 > 0 && DP[i - 2, 2] > 0)
            DP[i, 2] = DP[i - 2, 4];
 
        // If train C is present at
        // station i-3
        if (i - 3 > 0 && DP[i - 3, 3] > 0)
            DP[i, 3] = DP[i - 3, 4];
 
        // The total number of ways to
        // reach station i
        DP[i, 4] = (DP[i, 1] + DP[i, 2] + DP[i, 3]);
    }
 
    // Return the total count of ways
    return DP[N, 4];
}
 
// Driver Code
static public void Main()
{
    int N = 15;
    Console.WriteLine(numberOfWays(N));
}
}
 
// This code is contributed by Dharanendra L V.

Javascript




// Javascript program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the number of ways
// to reach Nth station
static int numberOfWays(int N)
{
    // Declares the DP[] array
    int DP[][] = new int[N + 1][5];
 
    // Initialize dp[][] array
    for (int i = 0; i < N +1; i++) {
        for (int j = 0; j < 5; j++) {
            DP[i][j] = 0;
        }
    }
 
    // Only 1 way to reach station 1
    DP[1][1] = 1;
    DP[1][2] = 1;
    DP[1][3] = 1;
    DP[1][4] = 1;
 
    // Find the remaining states from
    // the 2nd station
    for (int i = 2; i <= N; i++) {
 
        // If the train A is present
        // at station i - 1
        if (i - 1 > 0 && DP[i - 1][1] > 0)
            DP[i][1] = DP[i - 1][4];
 
        // If the train B is present
        // at station i-2
        if (i - 2 > 0 && DP[i - 2][2] > 0)
            DP[i][2] = DP[i - 2][4];
 
        // If train C is present at
        // station i-3
        if (i - 3 > 0 && DP[i - 3][3] > 0)
            DP[i][3] = DP[i - 3][4];
 
        // The total number of ways to
        // reach station i
        DP[i][4] = (DP[i][1] + DP[i][2]
                    + DP[i][3]);
    }
 
    // Return the total count of ways
    return DP[N][4];
}
 
// Driver Code
public static void main(String[] args)
{
     
    int N = 15;
    System.out.print(numberOfWays(N));
}
}
 
// This code is contributed by code_hunt.
Output: 
338

 

Time Complexity: O(N)
Auxiliary Space: O(N)

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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :