TopCoder problem "BadClock" used in SRM 172 (Division I Level One , Division II Level Two)

Problem Statement


According to Lewis Carroll, a clock that has stopped is more accurate than one that is five minutes behind. He argues that the former is right twice a day, whereas the latter never shows the correct time. Then again, a clock that is always five minutes behind is in a sense perfectly accurate, and therefore an extraordinary specimen. More usually, a clock is ahead or behind because it runs at the wrong rate, so that its absolute discrepancy from the true time is steadily changing. If left unregulated, such a clock will show the true time at regular but perhaps lengthy intervals.

You are given two Strings of the form "hh:mm:ss". The first represents exactly the true time, while the second is exactly the time shown by an ill-tuned clock. This is an analog clock whose hour, minute, and second hands sweep continuously around the dial at a speed that is too fast or too slow by a constant factor. Both times are given in the North American style, where the hour ranges between 1 and 12, inclusive. Given an int specifying the non-zero number of seconds that the clock gains every hour, calculate the number of hours that elapse before it agrees with the true time. Your answer, a double, must be correct with either absolute or relative precision of 1.0e-9 (one billionth).



Parameters:String, String, int
Method signature:double nextAgreement(String trueTime, String skewTime, int hourlyGain)
(be sure your method is public)


-If hourlyGain is negative, the clock falls behind by a fixed number of seconds every hour.
-It is not the case that the clock makes discrete jumps every so often. Rather, the hands of the clock are moving smoothly and continuously at a constant rate that is too slow or too fast relative to the true time.


-trueTime and skewTime each contain exactly eight characters in the format "hh:mm:ss", where the substring "hh" is a zero-padded integer between 1 and 12, inclusive, and "mm" and "ss" are zero-padded integers between 0 and 59, inclusive
-hourlyGain is either between -1800 and -1, inclusive, or between 1 and 3600, inclusive


Returns: 0.0
The clock is already showing the true time.
Returns: 70.0
This clock loses three seconds every hour, and will catch up with the true time in exactly 70 hours.
Returns: 70.0
This clock gains three seconds per hour.
Returns: 15.0
Returns: 8625.0
Returns: 22.42367601246106

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories: