Given an array date[] = [d, m, y], where d denotes the day, m denotes the month, and y denotes the year, Write a program that calculates the day of the week for any particular date in the past or future.
Examples:
Input: d = 30, m = 8, y = 2010
Output: 1
Explanation: 30th August 2010 was a Monday.Input: d = 15, m = 6, y = 1995
Output: 4
Explanation: 15th June 1995 was a Thursday.Input: d = 29, m = 2, y = 2016
Output: 1
Explanation: 26th January was a Monday.
Table of Content
[Naive Approach] Day Count Calculation - O(Y + M) Time and O(1) Space
Calculate total number of days from 01-01-0001 to given date. Since 01-01-0001 was Monday, map totalDays % 7 to day of week.
- Initialize totalDays = 0
- Add days of all complete years before given year
- Add days of all complete months before given month
- Add days of current month minus 1
- Use weekDays array starting with Monday
- Return weekDays[totalDays % 7]
#include <iostream>
#include <vector>
using namespace std;
bool isLeapYear(int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
string getDayOfWeek(vector<int>& date) {
int day = date[0];
int month = date[1];
int year = date[2];
vector<int> daysInMonth = {
31,28,31,30,31,30,
31,31,30,31,30,31
};
vector<string> weekDays = {
"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday"
};
long long totalDays = 0;
// Count complete years before current year
for (int y = 1; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
// Count complete months before current month
for (int m = 1; m < month; m++) {
if (m == 2 && isLeapYear(year))
totalDays += 29;
else
totalDays += daysInMonth[m - 1];
}
// Add days in current month
totalDays += day - 1;
// 01-01-0001 was Monday
return weekDays[totalDays % 7];
}
int main() {
vector<int> date = {17, 4, 1435};
cout << getDayOfWeek(date) << endl;
return 0;
}
// Java program to find day of week for a given date
import java.util.*;
class GfG {
static boolean isLeapYear(int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
static String getDayOfWeek(int[] date) {
int day = date[0];
int month = date[1];
int year = date[2];
int[] daysInMonth = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
String[] weekDays = {
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"
};
long totalDays = 0;
// Count complete years before current year
for (int y = 1; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
// Count complete months before current month
for (int m = 1; m < month; m++) {
if (m == 2 && isLeapYear(year))
totalDays += 29;
else
totalDays += daysInMonth[m - 1];
}
// Add days in current month
totalDays += day - 1;
// 01-01-0001 was Monday
return weekDays[(int)(totalDays % 7)];
}
public static void main(String[] args) {
int[] date = {17, 4, 1435};
System.out.println(getDayOfWeek(date));
}
}
# Python program to find day of week for a given date
def isLeapYear(year):
return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0)
def getDayOfWeek(date):
day, month, year = date[0], date[1], date[2]
daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
weekDays = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
totalDays = 0
# Count complete years before current year
for y in range(1, year):
totalDays += 366 if isLeapYear(y) else 365
# Count complete months before current month
for m in range(1, month):
if m == 2 and isLeapYear(year):
totalDays += 29
else:
totalDays += daysInMonth[m - 1]
# Add days in current month
totalDays += day - 1
# 01-01-0001 was Monday
return weekDays[totalDays % 7]
# Driver code
if __name__ == "__main__":
date = [17, 4, 1435]
print(getDayOfWeek(date))
// C# program to find day of week for a given date
using System;
class GfG {
static bool isLeapYear(int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
static string getDayOfWeek(int[] date) {
int day = date[0];
int month = date[1];
int year = date[2];
int[] daysInMonth = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
string[] weekDays = {
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"
};
long totalDays = 0;
// Count complete years before current year
for (int y = 1; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
// Count complete months before current month
for (int m = 1; m < month; m++) {
if (m == 2 && isLeapYear(year))
totalDays += 29;
else
totalDays += daysInMonth[m - 1];
}
// Add days in current month
totalDays += day - 1;
// 01-01-0001 was Monday
return weekDays[totalDays % 7];
}
static void Main(string[] args) {
int[] date = {17, 4, 1435};
Console.WriteLine(getDayOfWeek(date));
}
}
// JavaScript program to find day of week for a given date
function isLeapYear(year) {
return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0);
}
function getDayOfWeek(date) {
const day = date[0];
const month = date[1];
const year = date[2];
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const weekDays = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"];
let totalDays = 0;
// Count complete years before current year
for (let y = 1; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
// Count complete months before current month
for (let m = 1; m < month; m++) {
if (m === 2 && isLeapYear(year))
totalDays += 29;
else
totalDays += daysInMonth[m - 1];
}
// Add days in current month
totalDays += day - 1;
// 01-01-0001 was Monday
return weekDays[totalDays % 7];
}
// Driver code
const date = [17, 4, 1435];
console.log(getDayOfWeek(date));
Output
Friday
[Expected Approach] Zeller's Congruence Formula - O(1) Time and O(1) Space
Zeller's Congruence is a mathematical formula that calculates the day of the week for any given date. March is treated as the first month because it avoids complications caused by leap years. The formula uses month codes and year calculations to directly compute the day.
- Extract day (d), month (m), and year (y) from input
- If month is January or February, treat them as months of previous year because the formula is derived from a calendar system where the year is considered to start in March rather than January.
- Use month codes array t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
- Compute day number using formula: (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7
- Map result 0-6 to weekDays array starting with Sunday
#include <bits/stdc++.h>
using namespace std;
// Find the day of the week for the given date
string getDayOfWeek(vector<int> &date)
{
int d = date[0];
int m = date[1];
int y = date[2];
// Month codes used by the formula
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
// January and February are treated as months of previous year
y -= (m < 3);
// Calculate day number
int day = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
vector<string> weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return weekDays[day];
}
int main()
{
vector<int> date = {30, 8, 2010};
cout << getDayOfWeek(date);
return 0;
}
// Java program to find day of week using Tomohiko Sakamoto's algorithm
import java.util.*;
class Solution {
// Find the day of the week for the given date
public String getDayOfWeek(int[] date) {
int d = date[0];
int m = date[1];
int y = date[2];
// Month codes used by the formula
int[] t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
// January and February are treated as months of previous year
y -= (m < 3) ? 1 : 0;
// Calculate day number
int day = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
return weekDays[day];
}
}
public class Main {
public static void main(String[] args) {
Solution obj = new Solution();
int[] date = {30, 8, 2010};
System.out.println(obj.getDayOfWeek(date));
}
}
# Python program to find day of week using Tomohiko Sakamoto's algorithm
def getDayOfWeek(date):
d, m, y = date[0], date[1], date[2]
# Month codes used by the formula
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
# January and February are treated as months of previous year
if m < 3:
y -= 1
# Calculate day number
day = (y + y // 4 - y // 100 + y // 400 + t[m - 1] + d) % 7
weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
return weekDays[day]
# Driver code
if __name__ == "__main__":
date = [30, 8, 2010]
print(getDayOfWeek(date))
// C# program to find day of week using Tomohiko Sakamoto's algorithm
using System;
using System.Collections.Generic;
class GfG {
// Find the day of the week for the given date
static string getDayOfWeek(List<int> date) {
int d = date[0];
int m = date[1];
int y = date[2];
// Month codes used by the formula
int[] t = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
// January and February are treated as months of previous year
y -= (m < 3) ? 1 : 0;
// Calculate day number
int day = (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
string[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
return weekDays[day];
}
static void Main(string[] args) {
List<int> date = new List<int> { 30, 8, 2010 };
Console.WriteLine(getDayOfWeek(date));
}
}
// JavaScript program to find day of week using Tomohiko Sakamoto's algorithm
function getDayOfWeek(date) {
let d = date[0];
let m = date[1];
let y = date[2];
// Month codes used by the formula
let t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
// January and February are treated as months of previous year
if (m < 3) {
y -= 1;
}
// Calculate day number
let day = (y + Math.floor(y / 4) - Math.floor(y / 100) +
Math.floor(y / 400) + t[m - 1] + d) % 7;
let weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
return weekDays[day];
}
// Driver code
const date = [30, 8, 2010];
console.log(getDayOfWeek(date));
Output
Monday