Next Closest Time



@awice I think the following sentence accidentally has a repeated part:
"while times like 00:10 = 10 means that (10  720) % (24 * 60) = 710 % (24 * 60) = 730 seconds have elapsed."
I think you want to take out the first "% (24 * 60)" so it reads:
"while times like 00:10 = 10 means that (10  720) = 710 % (24 * 60) = 730 seconds have elapsed."Just trying to help, great answer! Thanks for the thorough explanation :)


@mahekjasani : I guess you are talking about the second approach. I think in this approach, candidate answers are not guaranteed to be generated in a serial order w.r.t time and allowed digit constraints. For example for "20:56", early return will be wrong answer of "22:50" instead of "22:00"

The following line in the 2nd solution seems to be overcomplicated and not necessary to use floored modulus or normal modulus (especially the floored modulus is not very common and hard to understand/explain/use):
int candElapsed = Math.floorMod(cur  start, 24 * 60);
Since
cur  start
can never be equal or bigger than24 * 60
, becausecur
can be as big as23:59
which is1439
, and start can be as small as00:00
which is0
(this example is not applicable in the solution anyways since none of those 4 digits matches, but just want to show the extreme case). So we only need to care about the negative cases (wherecur  start
is negative).In that case, the negative value can never be equal or smaller than
24 * 60
sincecur
can be as small as00:00
and start can be as big as23:59
, resulting in1439
, (again this extreme case is a bad example). so all in all I can just replace this modulus operation with:int candElapsed = (cur  start < 0) ? cur  start + 24 * 60 : cur  start;
to get back to the range of
0 < candElapsed < 1440