Scientist's Calender

Scientist loves to experiment. He always took problems from the real-world and then solves them himself. This time he was looking for holidays on the calendar and there he got a problem to solve. He wants to find the next calendar year which has exactly the same calendar as the current year.

Example:

Input:  n = 2005
Output: 2011

Approach

C++

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

int count(int n)
{
    if ((n % 400 == 0 || n % 100 != 0) && (n % 4 == 0))
        return 2;
    return 1;
}

int scienCalendar(int n)
{
    int day = count(n);
    int m = n + 1;
    int sum = 0;
    int ans = m;
    while (true)
    {
        sum = (sum + count(m)) % 7;
        if (sum == 0 && day == count(m))
        {
            ans = m;
            break;
        }
        m++;
    }
    return ans;
}
int main()
{
    int n = 2005;

    cout << scienCalendar(n<< "\n";

    return 0;
}


No comments:

Post a Comment