Thursday, 16 March 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 "

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

2 comments:

  1. Ghanendra Yadav thanks for updating

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

      Delete