Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

He Who Must Not Be Late

By RasheedAbid · Limits 1s, 512 MB

He Who Must Not Be Named, Lord Voldemort, is chasing Harry and Hermione. They have been able to reach the train station but, He Who Must Not Be Late, Ron, is running late. Now, Harry goes to the counter to ask about the tickets available later today. The man behind the counter gives Harry some rather "Riddikulus" information:

  1. In this world, the whole day is only 2H hours and the hour hand rotates 360 degrees every H hours (Similar to our world of 24 hours and the hour hand rotates 360 degrees every 12 hours). The first H hours is called AM and the second H hours of the day is called PM.

  2. Each H is made up of M minutes.

  3. Trains are only available, exactly when the hour hand and minute hand overlap. Train for a day starts at 00:00 AM.

Now Harry wants to know how long he has to wait before the next trains (all possible ones) arrive today.


The input consists of T test cases.

Each test case contains 4 integers H, M, CurrentHour and CurrentMinute and a string "AM" or "PM". CurrentHour and CurrentMinute refer to the time Harry asked about trains in the counter (The time took for the man to provide Harry all the information is negligible).

  • 1 <= T <= 100
  • 3 <= H <= 99
  • 10 <= M <= 99
  • 0 <= CurrentHour < H
  • 0 <= CurrentMinute < M


Print the case number and x, the number of upcoming trains today. The next x lines will contain the time Harry has to wait if he and Hermione want to catch that train, in hours and minutes (look in the sample test cases for clarification). Round the result to the nearest case if it has a decimal in it.


12 60 10 15 AM
12 60 4 15 PM
Case 1: 12
0h 40m
1h 45m
2h 50m
3h 56m
5h 1m
6h 7m
7h 12m
8h 18m
9h 23m
10h 29m
11h 34m
12h 40m
Case 2: 7
0h 7m
1h 12m
2h 18m
3h 23m
4h 29m
5h 34m
6h 40m



32% Solution Ratio

rebornEarliest, Sep '19

rebornFastest, 0.0s

rebornLightest, 131 kB

CoderMShortest, 674B


Login to submit

Toph uses cookies. By continuing you agree to our Cookie Policy.