Find the first day of a given year from a base year having first day as Monday

• Difficulty Level : Medium
• Last Updated : 04 May, 2021

Given two integers Y and B representing two years, the task is to find the day of the week in which 1st January of the year Y lies assuming that 1st January of the year B was a Monday.

Examples:

Input:
Y = 2020
B = 1900
Output:
Wednesday
Explanation:
01/01/2020 was a Wednesday considering that 01/01/1900 was a Monday

Input:
Y = 2020
B = 1905
Output:
Thursday
Explanation:
01/01/2020 was a Wednesday assuming that 01/01/1905 was a Monday

Approach: Follow the steps below to solve the problem:

1. Total years lying between base year (B) and the year (Y) is equal to (Y – 1) – B.
2. Total number of leap years lying in between = Total years / 4
3. Total number of non-leap years in between = Total Years – Leap Years.
4. Total Days = Total Leap Years * 366 + Non-Leap Years * 365 + 1.
5. Therefore, the day of the 1st January of Y is Total Days % 7.

Below is the implementation of the above approach:

C++14

 // C++ Implementation of// the above approach #include using namespace std; // Function to find the day of// 1st January of Y yearvoid findDay(int Y, int B){    int lyear, rest, totaldays, day;     // Count years between    // years Y and B    Y = (Y - 1) - B;     // Count leap years    lyear = Y / 4;     // Non leap years    rest = Y - lyear;     // Total number of days in the years    // lying between the years Y and B    totaldays = (rest * 365)                + (lyear * 366) + 1;     // Actual day    day = (totaldays % 7);     if (day == 0)        printf("Monday");     else if (day == 1)        printf("Tuesday");     else if (day == 2)        printf("Wednesday");     else if (day == 3)        printf("Thursday");     else if (day == 4)        printf("Friday");     else if (day == 5)        printf("Saturday");     else if (day == 6)        printf("Sunday");     else        printf("INPUT YEAR IS WRONG!");} // Driver Codeint main(){    int Y = 2020, B = 1900;    findDay(Y, B);     return 0;}

Java

 // Java program to implement// the above approachimport java.util.*; class GFG{   // Function to find the day of  // 1st January of Y year  static void findDay(int Y, int B)  {    int lyear, rest, totaldays, day;     // Count years between    // years Y and B    Y = (Y - 1) - B;     // Count leap years    lyear = Y / 4;     // Non leap years    rest = Y - lyear;     // Total number of days in the years    // lying between the years Y and B    totaldays = (rest * 365)      + (lyear * 366) + 1;     // Actual day    day = (totaldays % 7);     if (day == 0)      System.out.println("Monday");     else if (day == 1)      System.out.println("Tuesday");     else if (day == 2)      System.out.println("Wednesday");     else if (day == 3)      System.out.println("Thursday");     else if (day == 4)      System.out.println("Friday");     else if (day == 5)      System.out.println("Saturday");     else if (day == 6)      System.out.println("Sunday");     else      System.out.println("INPUT YEAR IS WRONG!");  }   // Driver code  public static void main(String[] args)  {    int Y = 2020, B = 1900;    findDay(Y, B);  }} // This code is contributed by code_hunt.

Python3

 # Python program to implement# the above approach # Function to find the day of# 1st January of Y yeardef findDay(Y, B):    lyear, rest, totaldays, day = 0, 0, 0, 0;     # Count years between    # years Y and B    Y = (Y - 1) - B;     # Count leap years    lyear = Y // 4;     # Non leap years    rest = Y - lyear;     # Total number of days in the years    # lying between the years Y and B    totaldays = (rest * 365) + (lyear * 366) + 1;     # Actual day    day = (totaldays % 7);     if (day == 0):        print("Monday");     elif (day == 1):        print("Tuesday");     elif (day == 2):        print("Wednesday");     elif (day == 3):        print("Thursday");     elif (day == 4):        print("Friday");     elif (day == 5):        print("Saturday");     elif (day == 6):        print("Sunday");     else:        print("INPUT YEAR IS WRONG!"); # Driver codeif __name__ == '__main__':    Y = 2020; B = 1900;    findDay(Y, B); # This code is contributed by 29AjayKumar

C#

 // C# program to implement// the above approachusing System;class GFG{  // Function to find the day of  // 1st January of Y year  static void findDay(int Y, int B)  {    int lyear, rest, totaldays, day;     // Count years between    // years Y and B    Y = (Y - 1) - B;     // Count leap years    lyear = Y / 4;     // Non leap years    rest = Y - lyear;     // Total number of days in the years    // lying between the years Y and B    totaldays = (rest * 365)      + (lyear * 366) + 1;     // Actual day    day = (totaldays % 7);    if (day == 0)      Console.WriteLine("Monday");    else if (day == 1)      Console.WriteLine("Tuesday");    else if (day == 2)      Console.WriteLine("Wednesday");    else if (day == 3)      Console.WriteLine("Thursday");    else if (day == 4)      Console.WriteLine("Friday");    else if (day == 5)      Console.WriteLine("Saturday");    else if (day == 6)      Console.WriteLine("Sunday");    else      Console.WriteLine("INPUT YEAR IS WRONG!");  } // Driver codestatic void Main(){    int Y = 2020, B = 1900;    findDay(Y, B);}} // This code is contribute by susmitakundugoaldanga

Javascript


Output:
Wednesday

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up