TCS NQT 2020 | Trains

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.00Output: 11.00 11.04 11.09 11.15 11.19 11.22Explanation: 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.15Output: Invalid InputExplanation: 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_Avoid 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 Codeint 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;}
11.00 11.04 11.09 11.15 11.19 11.22

Time Complexity: O(1)Auxiliary Space: O(1)
interview-preparation
prefix-sum
TCS
TCS-coding-questions
Arrays
Mathematical
TCS
prefix-sum
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays