Find the first day of a given year from a base year having first day as Monday
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:
- Total years lying between base year (B) and the year (Y) is equal to (Y – 1) – B.
- Total number of leap years lying in between = Total years / 4
- Total number of non-leap years in between = Total Years – Leap Years.
- Total Days = Total Leap Years * 366 + Non-Leap Years * 365 + 1.
- Therefore, the day of the 1st January of Y is Total Days % 7.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void findDay( int Y, int B)
{
int lyear, rest, totaldays, day;
Y = (Y - 1) - B;
lyear = Y / 4;
rest = Y - lyear;
totaldays = (rest * 365)
+ (lyear * 366) + 1;
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!" );
}
int main()
{
int Y = 2020, B = 1900;
findDay(Y, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findDay( int Y, int B)
{
int lyear, rest, totaldays, day;
Y = (Y - 1 ) - B;
lyear = Y / 4 ;
rest = Y - lyear;
totaldays = (rest * 365 )
+ (lyear * 366 ) + 1 ;
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!" );
}
public static void main(String[] args)
{
int Y = 2020 , B = 1900 ;
findDay(Y, B);
}
}
|
Python3
def findDay(Y, B):
lyear, rest, totaldays, day = 0 , 0 , 0 , 0 ;
Y = (Y - 1 ) - B;
lyear = Y / / 4 ;
rest = Y - lyear;
totaldays = (rest * 365 ) + (lyear * 366 ) + 1 ;
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!" );
if __name__ = = '__main__' :
Y = 2020 ; B = 1900 ;
findDay(Y, B);
|
C#
using System;
class GFG
{
static void findDay( int Y, int B)
{
int lyear, rest, totaldays, day;
Y = (Y - 1) - B;
lyear = Y / 4;
rest = Y - lyear;
totaldays = (rest * 365)
+ (lyear * 366) + 1;
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!" );
}
static void Main()
{
int Y = 2020, B = 1900;
findDay(Y, B);
}
}
|
Javascript
<script>
function findDay(Y, B)
{
let lyear, rest, totaldays, day;
Y = (Y - 1) - B;
lyear = Math.floor(Y / 4);
rest = Y - lyear;
totaldays = (rest * 365)
+ (lyear * 366) + 1;
day = (totaldays % 7);
if (day == 0)
document.write( "Monday" );
else if (day == 1)
document.write( "Tuesday" );
else if (day == 2)
document.write( "Wednesday" );
else if (day == 3)
document.write( "Thursday" );
else if (day == 4)
document.write( "Friday" );
else if (day == 5)
document.write( "Saturday" );
else if (day == 6)
document.write( "Sunday" );
else
document.write( "INPUT YEAR IS WRONG!" );
}
let Y = 2020, B = 1900;
findDay(Y, B);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...