Given a year .Your task is to find the number of every day in a year ie.number of Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday in that given year.
Examples:
Input: 2019
Output: Monday-52
Tuesday-53
Wednesday-52
Thursday-52
Friday-52
Saturday-52
Sunday-52
Input: 2024
Output: Monday-53
Tuesday-53
Wednesday-52
Thursday-52
Friday-52
Saturday-52
Sunday-52
Observations: We have to make some key observations. The first one will be that there are at least 52 weeks in a year, so every day will occur at least 52 times in a year. As 52*7 is 364 so the day occurring on the 1st January of any year will occur 53 times and if the year is a leap year then the day on the 2nd January will also occur 53 times.
Approach: Create a list with size 7 and having an initial value of 52 as the minimum number of occurrences will be 52. Find the index of the first day. Calculate the number of days whose occurrence will be 53.
Below is the implementation.
C++
#include <iostream>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
void day_occur_time( int year) {
string days[] = { "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" , "Sunday" };
int L[7];
for ( int i = 0; i < 7; i++) {
L[i] = 52;
}
int pos = -1;
time_t t = time (NULL);
tm * timePtr = localtime (&t);
timePtr->tm_year = year - 1900;
timePtr->tm_mon = 0;
timePtr->tm_mday = 1;
mktime (timePtr);
char buffer[80];
strftime (buffer, 80, "%A" , timePtr);
string day(buffer);
for ( int i = 0; i < 7; i++) {
if (day == days[i]) {
pos = i;
}
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
L[pos] += 1;
L[(pos+1)%7] += 1;
} else {
L[pos] += 1;
}
for ( int i = 0; i < 7; i++) {
cout << days[i] << " " << L[i] << endl;
}
}
int main() {
int year = 2019;
day_occur_time(year);
return 0;
}
|
Java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DayOccurTime {
public static void dayOccurTime( int year) {
String[] days = { "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" };
int [] L = new int [ 7 ];
for ( int i = 0 ; i < 7 ; i++) {
L[i] = 52 ;
}
LocalDate date = LocalDate.of(year, 1 , 1 );
String day = date.format(DateTimeFormatter.ofPattern( "EEEE" ));
int pos = - 1 ;
for ( int i = 0 ; i < 7 ; i++) {
if (day.equals(days[i])) {
pos = i;
}
}
if (date.isLeapYear()) {
L[pos] += 1 ;
L[(pos + 1 ) % 7 ] += 1 ;
} else {
L[pos] += 1 ;
}
for ( int i = 0 ; i < 7 ; i++) {
System.out.println(days[i] + " " + L[i]);
}
}
public static void main(String[] args) {
int year = 2019 ;
dayOccurTime(year);
}
}
|
Python3
import datetime
import calendar
def day_occur_time(year):
days = [ "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" ,
"Sunday" ]
L = [ 52 for i in range ( 7 )]
pos = - 1
day = datetime.datetime(year, month = 1 , day = 1 ).strftime( "%A" )
for i in range ( 7 ):
if day = = days[i]:
pos = i
if calendar.isleap(year):
L[pos] + = 1
L[(pos + 1 ) % 7 ] + = 1
else :
L[pos] + = 1
for i in range ( 7 ):
print (days[i], L[i])
year = 2019
day_occur_time(year)
|
C#
using System;
using System.Globalization;
public class DayOccurTime
{
public static void dayOccurTime( int year)
{
string [] days = { "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" };
int [] L = new int [7];
for ( int i = 0; i < 7; i++)
{
L[i] = 52;
}
DateTime date = new DateTime(year, 1, 1);
string day = date.ToString( "dddd" , CultureInfo.InvariantCulture);
int pos = Array.IndexOf(days, day);
if (DateTime.IsLeapYear(year))
{
L[pos] += 1;
L[(pos + 1) % 7] += 1;
}
else
{
L[pos] += 1;
}
for ( int i = 0; i < 7; i++)
{
Console.WriteLine(days[i] + " " + L[i]);
}
}
public static void Main( string [] args)
{
int year = 2019;
dayOccurTime(year);
}
}
|
Javascript
function day_occur_time(year) {
let days = [ "Monday" , "Tuesday" , "Wednesday" ,
"Thursday" , "Friday" , "Saturday" , "Sunday" ];
let L = new Array(7).fill(52);
let pos = -1;
let date = new Date(year, 0, 1);
let day = date.toLocaleDateString( 'en-US' , {weekday: 'long' });
for (let i = 0; i < 7; i++) {
if (day == days[i]) {
pos = i;
}
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
L[pos] += 1;
L[(pos+1)%7] += 1;
} else {
L[pos] += 1;
}
for (let i = 0; i < 7; i++) {
console.log(days[i] + " " + L[i]);
}
}
let year = 2019;
day_occur_time(year);
|
Output
Monday 52
Tuesday 53
Wednesday 52
Thursday 52
Friday 52
Saturday 52
Sunday 52
Time complexity: O(1)
Auxiliary space: O(1)