Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Roy and Trains problem solution

YASH PAL, 31 July 202413 February 2026
In this HackeEarth Roy and Trains problem solution Roy and Alfi reside in two different cities, Roy in city A and Alfi in city B. Roy wishes to meet her in city B.
 
There are two trains available from city A to city B. Roy is on his way to station A (Railway station of city A). It will take To time (in minutes) for Roy to reach station A. The two trains depart in T1 and T2 minutes respectively. The average velocities (in km/hr) of trains are V1 and V2 respectively. We know the distance D (in km) between city A and city B. Roy wants to know the minimum time (in minutes) to reach city B if he chooses to train optimally. If it’s not possible to reach city B, print “-1” instead (without quotes).
 
 
HackerEarth Roy and Trains problem solution

 

 

HackerEarth Roy and Trains problem solution.

def roy_and_trains():
for t in xrange(input()):
t0,t1,t2,v1,v2,d = map(int,raw_input().split())
temp = float(d)/v1
temp *= 60
temp2 = int(temp)
if temp > temp2:
temp2 += 1
total1 = t1 + temp2
temp = float(d)/v2
temp *= 60
temp2 = int(temp)
if temp > temp2:
temp2 += 1
total2 = t2 + temp2
if t0 > t1 and t0 > t2:
print "-1"
elif t0 <= t1 and t0 > t2:
print total1
elif t0 > t1 and t0 <= t2:
print total2
else:
print min(total1,total2)
roy_and_trains()
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes