Working of a 24 hour clock.
Given the current time of a 24 hour clock in the format hhmm, return the maximal time it could display in s seconds.
This was supposed to be the easiest problem here. I actually did get the idea for this one while in class getting bored but I am not sure if a codechef long contest was involved or not. xD
Pretty straightforward problem, with plenty of edge cases, one of which is:
1 2359 10
If it makes you feel any better, I failed at it too. (yay, problem testing)
The idea here is that while the 24 hour clock displays the hours and minutes, it hides the seconds, which could be anywhere between 00 and 59.
This difference of 60 seconds can produce 2 seperate times if you consider the time displayed by the clock S seconds later. For example, for the given test case:
1 1234 123
the clock could be 12:34:59 initially and hence 123 seconds later it will have, 12:37:22, leading to a display of 1237.
We can simply measure time at both ends, adjust for day changes and arrive at an answer.
ans in seconds = max((hh * 3600 + mm * 60 + s) % 86400, (hh * 3600 + mm * 60 + s + 59) % 86400)
Now, it is simply a matter of converting back to hours and minutes.
Which gives us a complete code (Python) of:
for _ in xrange(input()): hhmm = int(raw_input()) hh, mm = hhmm / 100, hhmm % 100 s = input() s = max((hh * 3600 + mm * 60 + s) % 86400, (hh * 3600 + mm * 60 + s + 59) % 86400) hh, mm = s / 3600, (s % 3600) / 60 print str(hh).zfill(2) + str(mm).zfill(2)