TCS NQT 2020 | Trains

• Difficulty Level : Medium
• Last Updated : 10 Jan, 2023

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”.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` ` `  `// 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; ` `} ` ` `  `// This code is contributed by aditya942003patil`

C

 `// C program for the above approach ` ` `  `#include ` `#include ` ` `  `// 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; ` `} `

Java

 `// Java program for the above approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `  ``// Function to find the timings for ` `  ``// train B having same time difference ` `  ``// as train_A ` `  ``static` `void` `findTime(``double``[] train_A, ``int` `N, ` `                       ``double` `t) ` `  ``{ ` `    ``double` `x; ` ` `  `    ``// Stores the time for train_B ` `    ``double``[] train_B = ``new` `double``[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 ` `        ``System.out.print(String.format(``"%.2f"``, x) + ``" "``); ` `        ``t = x; ` `      ``} ` `    ``} ` ` `  `    ``// If no answer exist ` `    ``else` `{ ` `      ``System.out.println(``"Invalid Input."``); ` `    ``} ` `  ``} ` ` `  `  ``// Driver Code ` `  ``public` `static` `void` `main(String[] args) ` `  ``{ ` `    ``// Given timings of train A ` `    ``// at each station ` `    ``double``[] train_A ` `      ``= { ``10.00``, ``10.04``, ``10.09``, ` `         ``10.15``, ``10.19``, ``10.22` `}; ` ` `  `    ``int` `N = train_A.length; ` ` `  `    ``// Given start time t ` `    ``double` `t = ``11.00``; ` ` `  `    ``// Function Call ` `    ``findTime(train_A, N, t); ` `  ``} ` `} ` ` `  `// This code is contributed by phasing17`

Python3

 `# Python3 program for the above approach ` ` `  `# Function to find the timings for ` `# train B having same time difference ` `# as train_A ` `def` `findTime(train_A, N, t): ` ` `  `    ``# Stores the time for train_B ` `    ``train_B ``=` `[``None``] ``*` `N ` `    ``train_B[``0``] ``=` `0.00``; ` ` `  `    ``for` `i ``in` `range``(``1``, N): ` `        ``train_B[i] ``=` `train_A[i] ``-` `train_A[i ``-` `1``]; ` `     `  `    ``# Variables for typecasting ` `    ``it ``=` `int``(t); ` ` `  `    ``# Check if t is valid ` `    ``if` `(t >``=` `0.0` `and` `t <``=` `24.0` `and` `(t ``-` `it) <``=` `60.0``) : ` ` `  `        ``# Traverse from 0 to 5 ` `        ``for` `i ``in` `range``(``6``): ` ` `  `            ``# 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 ` `            ``print``(f``"{x:.2f}"``, end ``=` `" "``) ` `            ``t ``=` `x; ` `         `  `    ``# If no answer exist ` `    ``else``: ` `        ``print``(``"Invalid Input"``); ` `     `  `# Driver Code ` ` `  `# Given timings of train A ` `# at each station ` `train_A ``=` `[ ``10.00``, ``10.04``, ``10.09``, ``10.15``, ``10.19``, ``10.22` `]; ` ` `  `N ``=` `len``(train_A); ` ` `  `# Given start time t ` `t ``=` `11.00``; ` ` `  `# Function Call ` `findTime(train_A, N, t); ` ` `  `# This code is contributed by phasing17`

C#

 `// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `  ``// Function to find the timings for ` `  ``// train B having same time difference ` `  ``// as train_A ` `  ``static` `void` `findTime(``double``[] train_A, ``int` `N, ` `                       ``double` `t) ` `  ``{ ` `    ``double` `x; ` ` `  `    ``// Stores the time for train_B ` `    ``double``[] train_B = ``new` `double``[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 ` `        ``Console.Write(x.ToString(``"N2"``) + ``" "``); ` `        ``t = x; ` `      ``} ` `    ``} ` ` `  `    ``// If no answer exist ` `    ``else` `{ ` `      ``Console.WriteLine(``"Invalid Input."``); ` `    ``} ` `  ``} ` ` `  `  ``// Driver Code ` `  ``public` `static` `void` `Main(``string``[] args) ` `  ``{ ` `    ``// Given timings of train A ` `    ``// at each station ` `    ``double``[] train_A ` `      ``= { 10.00, 10.04, 10.09, ` `         ``10.15, 10.19, 10.22 }; ` ` `  `    ``int` `N = train_A.Length; ` ` `  `    ``// Given start time t ` `    ``double` `t = 11.00; ` ` `  `    ``// Function Call ` `    ``findTime(train_A, N, t); ` `  ``} ` `} ` ` `  `// This code is contributed by phasing17`

Javascript

 `// JS program for the above approach ` ` `  `// Function to find the timings for ` `// train B having same time difference ` `// as train_A ` `function` `findTime(train_A, N, t) ` `{ ` `    ``let x; ` ` `  `    ``// Stores the time for train_B ` `    ``let train_B = ``new` `Array(N); ` `    ``train_B[0] = 0.00; ` ` `  `    ``for` `(let i = 1; i < N; i++) { ` `        ``train_B[i] = train_A[i] - train_A[i - 1]; ` `    ``} ` ` `  `    ``// Variables for typecasting ` `    ``let it, ix; ` `    ``it = Math.floor(t); ` ` `  `    ``// Check if t is valid ` `    ``if` `(t >= 0.0 && t <= 24.0 && (t - it) <= 60.0) { ` ` `  `        ``// Traverse from 0 to 5 ` `        ``for` `(let i = 0; i < 6; i++) { ` ` `  `            ``// Update t ` `            ``x = t + train_B[i]; ` `            ``ix = Math.floor(x); ` ` `  `            ``if` `(x - ix >= 0.60) ` `                ``x = x + 0.40; ` `            ``if` `(x > 24.00) ` `                ``x = x - 24.0; ` ` `  `            ``// Print the current time ` `            ``process.stdout.write(x.toFixed(2) + ``" "``); ` `            ``t = x; ` `        ``} ` `    ``} ` ` `  `    ``// If no answer exist ` `    ``else` `{ ` `        ``console.log(``"Invalid Input"``); ` `    ``} ` `} ` ` `  `// Driver Code ` ` `  `// Given timings of train A ` `// at each station ` `let train_A = [ 10.00, 10.04, 10.09, 10.15, 10.19, 10.22 ]; ` ` `  `let N = train_A.length; ` ` `  `// Given start time t ` `let t = 11.00; ` ` `  `// Function Call ` `findTime(train_A, N, t); ` ` `  `// This code is contributed by phasing17`

Output:

`11.00 11.04 11.09 11.15 11.19 11.22`

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

My Personal Notes arrow_drop_up
Related Articles