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
[Naive Approach] Using Simulation - O(n) Time O(1) Space
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.
#include <iostream>
using namespace std;
// Check leap year
bool isLeap(int y)
{
return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
// Days in a month
int getDays(int m, int y)
{
if (m == 2)
return isLeap(y) ? 29 : 28;
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
return 31;
}
// Move to next date
void nextDate(int &d, int &m, int &y)
{
d++;
if (d > getDays(m, y))
{
d = 1;
m++;
}
if (m > 12)
{
m = 1;
y++;
}
}
// simulate day by day
int numOfDays(int d1, int m1, int y1, int d2, int m2, int y2)
{
int d = d1, m = m1, y = y1;
int count = 0;
// Move until we reach second date
while (d != d2 || m != m2 || y != y2)
{
nextDate(d, m, y);
count++;
}
return count;
}
// Driver Code
int main()
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
cout << numOfDays(d1, m1, y1, d2, m2, y2) << endl;
return 0;
}
import java.util.Scanner;
// Check leap year
public class GfG {
public static boolean isLeap(int y)
{
return (y % 400 == 0)
|| (y % 4 == 0 && y % 100 != 0);
}
// Days in a month
public static int getDays(int m, int y)
{
if (m == 2)
return isLeap(y) ? 29 : 28;
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
return 31;
}
// Move to next date
public static void nextDate(int[] date)
{
int d = date[0], m = date[1], y = date[2];
d++;
if (d > getDays(m, y)) {
d = 1;
m++;
}
if (m > 12) {
m = 1;
y++;
}
date[0] = d;
date[1] = m;
date[2] = y;
}
// simulate day by day
public static int numOfDays(int d1, int m1, int y1,
int d2, int m2, int y2)
{
int[] date = { d1, m1, y1 };
int count = 0;
// Move until we reach second date
while (!(date[0] == d2 && date[1] == m2
&& date[2] == y2)) {
nextDate(date);
count++;
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
System.out.println(
numOfDays(d1, m1, y1, d2, m2, y2));
}
}
# Check leap year
def isLeap(y):
return (y % 400 == 0) or (y % 4 == 0 and y % 100 != 0)
# Days in a month
def getDays(m, y):
if m == 2:
return 29 if isLeap(y) else 28
if m in [4, 6, 9, 11]:
return 30
return 31
# Move to next date
def nextDate(d, m, y):
d += 1
if d > getDays(m, y):
d = 1
m += 1
if m > 12:
m = 1
y += 1
return d, m, y
# simulate day by day
def numOfDays(d1, m1, y1, d2, m2, y2):
d, m, y = d1, m1, y1
count = 0
# Move until we reach second date
while (d, m, y) != (d2, m2, y2):
d, m, y = nextDate(d, m, y)
count += 1
return count
# Driver Code
if __name__ == "__main__":
d1, m1, y1 = 10, 4, 2001
d2, m2, y2 = 10, 5, 2001
print(numOfDays(d1, m1, y1, d2, m2, y2))
using System;
// Check leap year
public class GfG {
public static bool isLeap(int y)
{
return (y % 400 == 0)
|| (y % 4 == 0 && y % 100 != 0);
}
// Days in a month
public static int getDays(int m, int y)
{
if (m == 2)
return isLeap(y) ? 29 : 28;
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
return 31;
}
// Move to next date
public static void nextDate(ref int d, ref int m,
ref int y)
{
d++;
if (d > getDays(m, y)) {
d = 1;
m++;
}
if (m > 12) {
m = 1;
y++;
}
}
// simulate day by day
public static int numOfDays(int d1, int m1, int y1,
int d2, int m2, int y2)
{
int d = d1, m = m1, y = y1;
int count = 0;
// Move until we reach second date
while (d != d2 || m != m2 || y != y2) {
nextDate(ref d, ref m, ref y);
count++;
}
return count;
}
// Driver Code
public static void Main()
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
Console.WriteLine(
numOfDays(d1, m1, y1, d2, m2, y2));
}
}
// Check leap year
function isLeap(y)
{
return (y % 400 === 0)
|| (y % 4 === 0 && y % 100 !== 0);
}
// Days in a month
function getDays(m, y)
{
if (m === 2)
return isLeap(y) ? 29 : 28;
if ([ 4, 6, 9, 11 ].includes(m))
return 30;
return 31;
}
// Move to next date
function nextDate(d, m, y)
{
d++;
if (d > getDays(m, y)) {
d = 1;
m++;
}
if (m > 12) {
m = 1;
y++;
}
return [ d, m, y ];
}
// simulate day by day
function numOfDays(d1, m1, y1, d2, m2, y2)
{
let [d, m, y] = [ d1, m1, y1 ];
let count = 0;
// Move until we reach second date
while (![d, m, y].every(
(val, idx) => val === [ d2, m2, y2 ][idx])) {
[d, m, y] = nextDate(d, m, y);
count++;
}
return count;
}
// Driver Code
let d1 = 10, m1 = 4, y1 = 2001;
let d2 = 10, m2 = 5, y2 = 2001;
console.log(numOfDays(d1, m1, y1, d2, m2, y2));
Output
30
Time Complexity: O(n)
Auxiliary Space: O(1)
[Expected Approach] Absolute Day Count - O(1) Time O(1) Space
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).
#include <iostream>
using namespace std;
// Number of days in each month (non-leap year)
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Count leap years before the given date
int countLeapYears(int d, int m, int y)
{
// If current month is Jan/Feb,
// current year is not counted
if (m <= 2)
y--;
return y / 4 - y / 100 + y / 400;
}
// Returns number of days between two dates
int numOfDays(int d1, int m1, int y1, int d2, int m2, int y2)
{
// Total days before first date
int n1 = y1 * 365 + d1;
for (int i = 0; i < m1 - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(d1, m1, y1);
// Total days before second date
int n2 = y2 * 365 + d2;
for (int i = 0; i < m2 - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2, m2, y2);
// Difference between both dates
return abs(n2 - n1);
}
// Driver Code
int main()
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
cout << numOfDays(d1, m1, y1, d2, m2, y2) << endl;
return 0;
}
public class GfG {
// Number of days in each month (non-leap year)
static final int[] monthDays = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
// Count leap years before the given date
static int countLeapYears(int d, int m, int y)
{
// If current month is Jan/Feb,
// current year is not counted
if (m <= 2)
y--;
return y / 4 - y / 100 + y / 400;
}
// Returns number of days between two dates
static int numOfDays(int d1, int m1, int y1, int d2,
int m2, int y2)
{
// Total days before first date
int n1 = y1 * 365 + d1;
for (int i = 0; i < m1 - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(d1, m1, y1);
// Total days before second date
int n2 = y2 * 365 + d2;
for (int i = 0; i < m2 - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2, m2, y2);
// Difference between both dates
return Math.abs(n2 - n1);
}
public static void main(String[] args)
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
System.out.println(
numOfDays(d1, m1, y1, d2, m2, y2));
}
}
from datetime import datetime
# Number of days in each month (non-leap year)
monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Count leap years before the given date
def countLeapYears(d, m, y):
# If current month is Jan/Feb,
# current year is not counted
if m <= 2:
y -= 1
return y // 4 - y // 100 + y // 400
# Returns number of days between two dates
def numOfDays(d1, m1, y1, d2, m2, y2):
# Total days before first date
n1 = y1 * 365 + d1
for i in range(m1 - 1):
n1 += monthDays[i]
n1 += countLeapYears(d1, m1, y1)
# Total days before second date
n2 = y2 * 365 + d2
for i in range(m2 - 1):
n2 += monthDays[i]
n2 += countLeapYears(d2, m2, y2)
# Difference between both dates
return abs(n2 - n1)
# Driver Code
if __name__ == "__main__":
d1, m1, y1 = 10, 4, 2001
d2, m2, y2 = 10, 5, 2001
print(numOfDays(d1, m1, y1, d2, m2, y2))
using System;
public class GfG {
// Number of days in each month (non-leap year)
static readonly int[] monthDays = { 31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31 };
// Count leap years before the given date
static int countLeapYears(int d, int m, int y)
{
// If current month is Jan/Feb,
// current year is not counted
if (m <= 2)
y--;
return y / 4 - y / 100 + y / 400;
}
// Returns number of days between two dates
static int numOfDays(int d1, int m1, int y1, int d2,
int m2, int y2)
{
// Total days before first date
int n1 = y1 * 365 + d1;
for (int i = 0; i < m1 - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(d1, m1, y1);
// Total days before second date
int n2 = y2 * 365 + d2;
for (int i = 0; i < m2 - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2, m2, y2);
// Difference between both dates
return Math.Abs(n2 - n1);
}
static void Main()
{
int d1 = 10, m1 = 4, y1 = 2001;
int d2 = 10, m2 = 5, y2 = 2001;
Console.WriteLine(
numOfDays(d1, m1, y1, d2, m2, y2));
}
}
"use strict";
// Number of days in each month (non-leap year)
const monthDays =
[ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// Count leap years before the given date
function countLeapYears(d, m, y)
{
// If current month is Jan/Feb,
// current year is not counted
if (m <= 2)
y--;
return Math.floor(y / 4) - Math.floor(y / 100)
+ Math.floor(y / 400);
}
// Returns number of days between two dates
function numOfDays(d1, m1, y1, d2, m2, y2)
{
// Total days before first date
let n1 = y1 * 365 + d1;
for (let i = 0; i < m1 - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(d1, m1, y1);
// Total days before second date
let n2 = y2 * 365 + d2;
for (let i = 0; i < m2 - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(d2, m2, y2);
// Difference between both dates
return Math.abs(n2 - n1);
}
// Driver Code
let d1 = 10, m1 = 4, y1 = 2001;
let d2 = 10, m2 = 5, y2 = 2001;
console.log(numOfDays(d1, m1, y1, d2, m2, y2));
Output
30
Time Complexity: O(1)
Auxiliary Space: O(1)