![]() |
VOOZH | about |
Given two dates, find the total number of days between them.
Examples:
Input:
d1 = 10, m1 = 4, y1 = 2013
d2 = 14, m2 = 4, y2 = 2013
Output: 4
Explanation: By counting manually, we find out there are 4 days between the two dates.
Input:
d1 = 10, m1 = 4, y1 = 2001
d2 = 10, m2 = 5, y2 = 2001
Output: 30
Explanation: By counting manually, we find out there are 30 days between the two dates.
Table of Content
The idea is to simulate the calendar one day at a time starting from the first date and keep moving forward until we reach the second date. Each step represents one day, and we count how many steps are needed.
30
Time Complexity: O(n)
Auxiliary Space: O(1)
The idea is to count all years divisible by 4, then remove the years which are divisible by 100 (since they are not leap years), and finally add back the years which are divisible by 400 (since they are still leap years).
30
Time Complexity: O(1)
Auxiliary Space: O(1)