16/03/2017

Geeksforgeeks Solution For " Find number of days between two given dates "

GeeksforGeeks Solution For School Domain .Below You Can Find The Solution Of Basic ,Easy ,Medium ,Hard .You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Given two dates, find total number of days between them. The count of days must be calculated in O(1) time and O(1) auxiliary space.

Note :- The system follows Gregorian calendar from the beginning of the time.

Submit Your Solution :- Click Here 


Solution :-

#include<bits/stdc++.h>
using namespace std;

struct Date
{
int d, m, y;
};
const int monthDays[12] = {31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31};

int countLeapYears(Date d)
{
int years = d.y;
if (d.m <= 2)
years--;
return years / 4 - years / 100 + years / 400;
}
int getDifference(Date dt1, Date dt2)
{
long int n1 = dt1.y*365 + dt1.d;
for (int i=0; i<dt1.m - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(dt1);

long int n2 = dt2.y*365 + dt2.d;
for (int i=0; i<dt2.m - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(dt2);
return (n2 - n1);
}

int main()
{
    int t,d,m,y;
    cin>>t;
    while(t--)
    {
Date dt1,dt2;
    cin>>d>>m>>y;
    dt1={d,m,y};
    cin>>d>>m>>y;
    dt2={d,m,y};
cout<<abs(getDifference(dt1, dt2))<<endl;
    }
return 0;
}

Output:-

Geeksforgeeks Solution For " Find number of days between two given dates "

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

2 comments:

  1. Ghanendra Yadav thanks for updating

    ReplyDelete
    Replies
    1. Thanks For Comment. Happy 2 Help Please share with friends Thanks

      Delete