Drill

ProblemsC# › events

How long is this event in minutes

easyeventsMathParsingC#

An event start and end are given as four-digit HHMM integers. Return the duration in minutes.

EventLengthMins(startHhmm: int, endHhmm: int) → int

C# 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.

Solve it in Python →

Where you start

public int EventLengthMins(int startHhmm, int endHhmm) {
    
}

Worked examples

CallResult
EventLengthMins(930, 1130)120
EventLengthMins(0, 100)60
EventLengthMins(2300, 100)120
EventLengthMins(1200, 1200)0

Hint

Convert each to minutes (hh × 60 + mm); the answer is the difference mod 1440, so add 1440 when it comes out negative.

Reference solution in C#
public int EventLengthMins(int startHhmm, int endHhmm) {
    int sMin = (startHhmm / 100) * 60 + (startHhmm % 100);
    int eMin = (endHhmm / 100) * 60 + (endHhmm % 100);
    int diff = eMin - sMin;
    return ((diff % 1440) + 1440) % 1440;
}

The same problem in another language

More events problems in C#