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++
#include <bits/stdc++.h>
void findTime( float train_A[], int N,
float t)
{
float x;
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];
}
int it, ix;
it = ( int )t;
if (t >= 0.0 && t <= 24.0 && (t - it) <= 60.0) {
for ( int i = 0; i < 6; i++) {
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;
printf ( "%.2f " , x);
t = x;
}
}
else {
printf ( "Invalid Input" );
}
}
int main()
{
float train_A[]
= { 10.00, 10.04, 10.09,
10.15, 10.19, 10.22 };
int N = sizeof (train_A)
/ sizeof (train_A[0]);
float t = 11.00;
findTime(train_A, N, t);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void findTime( float train_A[], int N,
float t)
{
float x;
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];
}
int it, ix;
it = ( int )t;
if (t >= 0.0 && t <= 24.0
&& (t - it) <= 60.0) {
for ( int i = 0; i < 6; i++) {
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;
printf ( "%.2f " , x);
t = x;
}
}
else {
printf ( "Invalid Input" );
}
}
int main()
{
float train_A[]
= { 10.00, 10.04, 10.09,
10.15, 10.19, 10.22 };
int N = sizeof (train_A)
/ sizeof (train_A[0]);
float t = 11.00;
findTime(train_A, N, t);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findTime( double [] train_A, int N,
double t)
{
double x;
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 ];
}
int it, ix;
it = ( int )t;
if (t >= 0.0 && t <= 24.0 && (t - it) <= 60.0 ) {
for ( int i = 0 ; i < 6 ; i++) {
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 ;
System.out.print(String.format( "%.2f" , x) + " " );
t = x;
}
}
else {
System.out.println( "Invalid Input." );
}
}
public static void main(String[] args)
{
double [] train_A
= { 10.00 , 10.04 , 10.09 ,
10.15 , 10.19 , 10.22 };
int N = train_A.length;
double t = 11.00 ;
findTime(train_A, N, t);
}
}
|
Python3
def findTime(train_A, N, t):
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 ];
it = int (t);
if (t > = 0.0 and t < = 24.0 and (t - it) < = 60.0 ) :
for i in range ( 6 ):
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 (f "{x:.2f}" , end = " " )
t = x;
else :
print ( "Invalid Input" );
train_A = [ 10.00 , 10.04 , 10.09 , 10.15 , 10.19 , 10.22 ];
N = len (train_A);
t = 11.00 ;
findTime(train_A, N, t);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findTime( double [] train_A, int N,
double t)
{
double x;
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];
}
int it, ix;
it = ( int )t;
if (t >= 0.0 && t <= 24.0 && (t - it) <= 60.0) {
for ( int i = 0; i < 6; i++) {
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;
Console.Write(x.ToString( "N2" ) + " " );
t = x;
}
}
else {
Console.WriteLine( "Invalid Input." );
}
}
public static void Main( string [] args)
{
double [] train_A
= { 10.00, 10.04, 10.09,
10.15, 10.19, 10.22 };
int N = train_A.Length;
double t = 11.00;
findTime(train_A, N, t);
}
}
|
Javascript
function findTime(train_A, N, t)
{
let x;
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];
}
let it, ix;
it = Math.floor(t);
if (t >= 0.0 && t <= 24.0 && (t - it) <= 60.0) {
for (let i = 0; i < 6; i++) {
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;
process.stdout.write(x.toFixed(2) + " " );
t = x;
}
}
else {
console.log( "Invalid Input" );
}
}
let train_A = [ 10.00, 10.04, 10.09, 10.15, 10.19, 10.22 ];
let N = train_A.length;
let t = 11.00;
findTime(train_A, N, t);
|
Output
11.00 11.04 11.09 11.15 11.19 11.22
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...