Convert seconds to hh:mm:ss parts

Convert seconds to hh:mm:ss parts

Hard Java Data Types 16 views
Explanation Complexity

Problem Statement

Task: given total seconds, return [hours, minutes, seconds].

Input Format

An integer totalSeconds.

Output Format

An array [hours, minutes, seconds].

Example

3665
[1, 1, 5]

Constraints

• totalSeconds ≥ 0

Concept Explanation

1 hour = 3600 seconds
1 minute = 60 seconds

We break total seconds into hours, then remaining minutes, then remaining seconds.

Step-by-Step Explanation

1.Read integer totalSeconds.

2.Compute hours = totalSeconds / 3600.

3.Compute remaining = totalSeconds % 3600.

4.Compute minutes = remaining / 60.

5.Compute seconds = remaining % 60.

6.Return [hours, minutes, seconds].

Concept Explanation

1 hour = 3600 seconds
1 minute = 60 seconds

We break total seconds into hours, then remaining minutes, then remaining seconds.

Step-by-Step Explanation

1.Read integer totalSeconds.

2.Compute hours = totalSeconds / 3600.

3.Compute remaining = totalSeconds % 3600.

4.Compute minutes = remaining / 60.

5.Compute seconds = remaining % 60.

6.Return [hours, minutes, seconds].

Input / Output Format

Input Format
An integer totalSeconds.
Output Format
An array [hours, minutes, seconds].
Constraints
• totalSeconds ≥ 0

Examples

Input:
3665
Output:
[1, 1, 5]

Example Solution (Public)

Java
static int[] toHMS(int sec){int h=sec/3600;sec%=3600;int m=sec/60;int s=sec%60;return new int[]{h,m,s};}

Official Solution Code

static int[] toHMS(int sec){int h=sec/3600;sec%=3600;int m=sec/60;int s=sec%60;return new int[]{h,m,s};}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.