Table of Contents
Problem Statement
“Seconds To Days” problem states that you are given an integer s referring to the seconds. Print the corresponding number of the days, hours, minutes, and seconds.
Example
s = 1543765
17dd 20hh 49mm 25ss
Explanation: Converting the given input into the required format. We get 17 d, 20 h, 49 m, and 25 s.
s = 9856743
114dd 1hh 59mm 3ss
Approach
Use the mathematical formula to calculate the following variables –
As we know 1 day = 24*3600 seconds
Therefore, to convert seconds into minutes –
numberOfDays = inputSeconds / 24*3600
As we know 1 hour = 3600 seconds
Therefore, to convert seconds into minutes –
numberOfHours = remainingSeconds / 3600
As we know 1 minute = 60 seconds
Therefore, to convert seconds into minutes –
numberOfMinutes = remianingSeconds / 60
Algorithm to convert seconds into days
1. Initialise a variable of interger type s referring to the seconds. 2. Create function to convert the seconds into the days, hours, minutes and seconds which accept an integer variable as it's parameter. 3. After that, create a variable of interger type days and initialise it's value as the result of the division of the given integer variable s and 24x3600. 4. Update the value of given integer variable s as the result of the mod of the given integer variable s and 24x3600. 5. Similarly, create a variable of interger type hours and initialise it's value as the result of the division of the given integer variable s and 3600. 6. Update the value of given integer variable s as the result of the mod of the given integer variable s and 3600. 7. Similarly, a variable of interger type minutes and initialise it's value as the result of the division of the given integer variable s and 60. 8. Update the value of given integer variable s as the result of the mod of the given integer variable s and 60. 9. After that, a variable of interger type variable seconds and initialise it as s. 10. Finally, print the integer variables days, hours, minutes and seconds.
Let’s take an example s = 300 seconds. Now first of all we will find the number of days. Because that is the unit of time given here which has a maximum number of seconds among others ( 1 day = 24 x 3600 s, 1 hour = 3600 s, 1 minute = 60 s). So, on dividing the s with j24 x 3600, we will get a quotient of 0. And a remainder of 300. So, we have 0 days. Now with the remaining seconds, we find the number of hours. Again we divide 300 with 3600. We get a quotient of 0 and remainder of 300. So now we find a number of minutes, by dividing the remaining seconds with 60. We get quotient as 5 and remainder as 0. So, after solving we get that 300 s = 0 days 0 hours 0 minutes 0 seconds.
Code
C++ Program to convert seconds into days
#include <bits/stdc++.h> using namespace std; void convertSecond(int s){ int days = s/(24*3600); s = s%(24*3600); int hours = s/3600; s %= 3600; int minutes = s/60; s %= 60; int seconds = s; cout<<days<<"dd "<<hours<<"hh "<<minutes<<"mm "<<seconds<<"ss "; } int main(){ int s = 1543765; convertSecond(s); return 0; }
17dd 20hh 49mm 25ss
Java Program to convert seconds into days
import java.io.*; class Seconds{ static void convertSecond(int s){ int days = s/(24*3600); s = s%(24*3600); int hours = s/3600; s %= 3600; int minutes = s/60; s %= 60; int seconds = s; System.out.println(days+"dd "+hours+"hh "+minutes+"mm "+seconds+"ss "); } public static void main (String[] args){ int s = 1543765; convertSecond(s); } }
17dd 20hh 49mm 25ss
Complexity Analysis
Time complexity
O(1) because we used constant time. We only some particular formulae and those too on a single input.
Space complexity
O(1) because we used constant space, even if needed temporary variables they were in a constant number.