Skip to content
Related Articles

Related Articles

Improve Article

TCS NQT 2020 | Trains

  • Difficulty Level : Medium
  • Last Updated : 29 Oct, 2020

Problem:

In one pass, Train A can start from the source station at time T[0], halt at each station for h unit of time until it reaches the last station at time T[N – 1], where N is the positive integer representing a total number of stations. 

Given, Train A’s timings at each unit of time as T[] = {10.00, 10.04, 10.09, 10.15, 10.19, 10.22}.

Now, suppose Railway Admin wants to add more trains to increase the frequency. So, to launch other Train B, for the same stations as of Train A’s. Provided the Train B starts at time t, they would like to know the timings for Train B. The program should return a String array S (timestamp(in float) for Train B at each station from first to the last station like train A).  

Note:



  • The time is represented in 24-Hour.
  • Start Hour should be in the range [0, 23].
  • Start Minute should be in the range [0, 59].
  • Enter start time(24 Hrs)

Examples:

Input: t = 11.00
Output: 11.00 11.04 11.09 11.15 11.19 11.22
Explanation: Start time for train B is 11.00 and also the time difference between the stations for train B is same as for train A.

Input: t = -26.15
Output: Invalid Input
Explanation: No such time as -26.15 exists. Hence, print “Invalid Input”.

Approach: The idea is to calculate the time differences between the stations from the given timings of Train A. Follow the steps below to solve the problem:

  • From the given array T[], generate an array train_B[] where train_B[i] is the time difference between T[i] and T[i – 1], where train_B[0] = 0.00 and 1 ≤ i ≤ 5.
  • Therefore, train_B[] = {0.00, 0.04, 0.05, 0.06, 0.04, 0.03}.
  • If the integer part of t is not in the range [0, 24] or the decimal part of t is not in the range [0, 60], then print “Invalid Input” because the integer part represents the hours and the decimal part represents the minutes.
  • Otherwise, traverse over the range [0, 5] and print t + train_B[i] denoting the time for train B for the ith station. Then update t as t = t + train_B[i].

Below is the implementation of the above approach:

C




// C program for the above approach
  
#include <stdio.h>
#include <string.h>
  
// Function to find the timings for
// train B having same time difference
// as train_A
void findTime(float train_A[], int N,
              float t)
{
    float x;
  
    // Stores the time for train_B
    float train_B[N];
    train_B[0] = 0.00;
  
    for (int i = 1; i < N; i++) {
        train_B[i] = train_A[i]
                     - train_A[i - 1];
    }
  
    // Variables for typecasting
    int it, ix;
    it = (int)t;
  
    // Check if t is valid
    if (t >= 0.0 && t <= 24.0
        && (t - it) <= 60.0) {
  
        // Traverse from 0 to 5
        for (int i = 0; i < 6; i++) {
  
            // Update t
            x = t + train_B[i];
            ix = (int)x;
  
            if (x - ix >= 0.60)
                x = x + 0.40;
            if (x > 24.00)
                x = x - 24.0;
  
            // Print the current time
            printf("%.2f ", x);
            t = x;
        }
    }
  
    // If no answer exist
    else {
        printf("Invalid Input");
    }
}
  
// Driver Code
int main()
{
    // Given timings of train A
    // at each station
    float train_A[]
        = { 10.00, 10.04, 10.09,
            10.15, 10.19, 10.22 };
  
    int N = sizeof(train_A)
            / sizeof(train_A[0]);
  
    // Given start time t
    float t = 11.00;
  
    // Function Call
    findTime(train_A, N, t);
    return 0;
}
Output:
11.00 11.04 11.09 11.15 11.19 11.22

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

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 :