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.
#include <iostream>
#include <ctime>
#include <cstring>
#include <cmath>
using namespace std;
void day_occur_time(int year) {
// stores days in a week
string days[] = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
// Initialize all counts as 52
int L[7];
for (int i = 0; i < 7; i++) {
L[i] = 52;
}
// Find the index of the first day
// of the year
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;
}
}
// mark the occurrence to be 53 of 1st day
// and 2nd day if the year is leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
L[pos] += 1;
L[(pos+1)%7] += 1;
} else {
L[pos] += 1;
}
// Print the days
for (int i = 0; i < 7; i++) {
cout << days[i] << " " << L[i] << endl;
}
}
int main() {
int year = 2019;
day_occur_time(year);
return 0;
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DayOccurTime {
public static void dayOccurTime(int year) {
// stores days in a week
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// Initialize all counts as 52
int[] L = new int[7];
for (int i = 0; i < 7; i++) {
L[i] = 52;
}
// Find the index of the first day of the year
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;
}
}
// mark the occurrence to be 53 of 1st day and 2nd day if the year is leap year
if (date.isLeapYear()) {
L[pos] += 1;
L[(pos + 1) % 7] += 1;
} else {
L[pos] += 1;
}
// Print the days
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);
}
}
// This code is contributed by divyansh2212
# python program Find number of
# times every day occurs in a Year
import datetime
import calendar
def day_occur_time(year):
# stores days in a week
days = [ "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday" ]
# Initialize all counts as 52
L = [52 for i in range(7)]
# Find the index of the first day
# of the year
pos = -1
day = datetime.datetime(year, month = 1, day = 1).strftime("%A")
for i in range(7):
if day == days[i]:
pos = i
# mark the occurrence to be 53 of 1st day
# and 2nd day if the year is leap year
if calendar.isleap(year):
L[pos] += 1
L[(pos+1)%7] += 1
else:
L[pos] += 1
# Print the days
for i in range(7):
print(days[i], L[i])
# Driver Code
year = 2019
day_occur_time(year)
using System;
using System.Globalization;
public class DayOccurTime
{
public static void dayOccurTime(int year)
{
// stores days in a week
string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
// Initialize all counts as 52
int[] L = new int[7];
for (int i = 0; i < 7; i++)
{
L[i] = 52;
}
// Find the index of the first day of the year
DateTime date = new DateTime(year, 1, 1);
string day = date.ToString("dddd", CultureInfo.InvariantCulture);
int pos = Array.IndexOf(days, day);
// mark the occurrence to be 53 of 1st day and 2nd day if the year is leap year
if (DateTime.IsLeapYear(year))
{
L[pos] += 1;
L[(pos + 1) % 7] += 1;
}
else
{
L[pos] += 1;
}
// Print the days
for (int i = 0; i < 7; i++)
{
Console.WriteLine(days[i] + " " + L[i]);
}
}
public static void Main(string[] args)
{
int year = 2019;
dayOccurTime(year);
}
}
function day_occur_time(year) {
// stores days in a week
let days = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" ];
// Initialize all counts as 52
let L = new Array(7).fill(52);
// Find the index of the first day
// of the year
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;
}
}
// mark the occurrence to be 53 of 1st day
// and 2nd day if the year is leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
L[pos] += 1;
L[(pos+1)%7] += 1;
} else {
L[pos] += 1;
}
// Print the days
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)