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:
- Take train A at station 1 and continue to station 4 using only train A as (A ? A ? A ? A).
- Take train B at station 1, stop at station 3 and take train A from station 3 to 4 as (B? . ? A).
- 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. |
338
Â
Time Complexity: O(N)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!