Seconds to HH:MM:SS

Easy · rating 1000 · Implementation, Math

A video is n seconds long. Read the integer n and print its duration formatted as HH:MM:SS, each field zero-padded to at least two digits.

For example, 3661 seconds is 01:01:01.

Constraints: 0 ≤ n ≤ 359999

Editorial

Approach

There are 3600 seconds in an hour and 60 in a minute. Peel off hours with integer division by 3600, then minutes from the remainder, and the leftover is seconds. Zero-pad each field to two digits.

n = int(input())
h, n = divmod(n, 3600)
m, s = divmod(n, 60)
print(f"{h:02d}:{m:02d}:{s:02d}")

Complexity

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

Related problems

Open Seconds to HH:MM:SS in Code Arena →