How many minutes between two clock times
Two timestamps on the same day: return the signed forward distance in minutes, wrapping past midnight.
- Both values are minutes since midnight.
- If endMinutes is after startMinutes the answer is straightforward.
- If endMinutes is at or before startMinutes the interval crosses midnight: add 1440 before subtracting.
- Midnight to midnight is 0.
hoursBetween(startMinutes: int, endMinutes: int) → int
Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
int hoursBetween(int startMinutes, int endMinutes) {
}
Worked examples
| Call | Result |
|---|---|
hoursBetween(0, 60) | 60 |
hoursBetween(60, 0) | 1380 |
hoursBetween(1400, 100) | 140 |
hoursBetween(0, 1440) | 0 |
Hint
Subtract, then add 1440 if the result is not positive, and take modulo 1440.
Reference solution in Java
int hoursBetween(int startMinutes, int endMinutes) {
int diff = endMinutes - startMinutes;
return ((diff % 1440) + 1440) % 1440;
}