Format a Video Length

Medium · rating 1100 · math, implementation

Read a non-negative integer number of seconds. Print it as H:MM:SS, where minutes and seconds are always two digits and hours have no leading zeros (e.g. 37251:02:05).

Constraints: 0 ≤ seconds ≤ 109

Editorial

Format a Video Length is the inverse: turn a second count into H:MM:SS, zero-padding minutes and seconds to two digits. Use integer division and modulo to peel off hours, then minutes, then seconds.

h = total // 3600
m = (total % 3600) // 60
s = total % 60
print(f'{h}:{m:02d}:{s:02d}')

Complexity

Time: O(1). Space: O(1).

Open Format a Video Length in Code Arena →