Given a date (day, month, year), the task is to determine the day of the week on which that date falls. The function should be able to compute the day for any date in the past or future. The function should return values from 0 to 6 where 0 means Sunday, 1 Monday and so on.
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.
Formula-based Approach
The Formula-Based Approach for calculating the day of the week is an efficient method that calculates the day using simple arithmetic operations. It leverages precomputed month codes and year codes to determine the day on a given date.
Note: This approach doesn't work for leap year days.
Formula:
dayOfWeek = (d + monthCode + yearCode) % 7
Steps for Calculation:
- Step 1: Calculate the Month Code: Each month has a predefined month code, and the value for each month is as follows:
- January = 6, February = 2 (1 for leap years), March = 2, April = 5, May = 0, June = 3, July = 5, August = 1, September = 4, October = 6, November = 2, December = 4
- Step 2: Calculate the Year Code: The year code is calculated based on the last two digits of the year. The steps are:
- Take the last two digits of the year. For 1995, the last two digits are 95.
- Multiply by 1.25: 95×1.25=118.75
- Take the floor of the result (i.e., the greatest integer less than or equal to the result)
- Apply modulo 7: 118%7=5
- If the year is a multiple of 400, use the last two digits as they are without any changes.
- Step 3: Apply Century Adjustment: Adjust for the century of the year:
- For years between 100 and 199: Add 5.
- For years between 200 and 299: Add 3.
- For years between 300 and 399: Add 1.
- This step is used to account for the shifting of days due to century years (e.g., 1800, 1900).
- Step 4: Calculate the Day of the Week
- Now that we have the date, month code, and year code, we simply sum them and take the result modulo 7 to determine the day of the week:
Time Complexity: O(1)
Auxiliary Space: O(1)
Zeller's Congruence-Based Approach
This approach calculates the day of the week for a given date using a formula involving the year, month, and day. It is essentially a simplified version of Zeller's Congruence, with adjustments for century years and leap years.
- Predefined Month Codes (
t[]): - The array
t[] stores month codes for each month. These values are predefined to account for the different lengths of each month. - For example: January = 6, February = 2 (or 1 for leap years), March = 2, etc.
- Adjusting the Year for January and February (
y -= m < 3): - The code adjusts the year if the month is January or February.
- This is because in Zeller's Congruence, January and February are treated as the 13th and 14th months of the previous year.
- If the month is January or February, it reduces the year by 1 (i.e.,
y -= 1).
- Leap Year Adjustments (
y / 4 - y / 100 + y / 400): The formula adjusts for leap years and century years:y / 4: Adds an adjustment for the leap years.y / 100: Subtracts the days for century years that are not leap years.y / 400: Adds back the leap days for century years that are divisible by 400 (e.g., 1600, 2000).
- Final Calculation:
- The formula: (y + y/4 − y/100 + y/400 + t[m−1] + d) % 7 calculates the day of the week by summing the day (
d), month code (t[m - 1]), and adjusted year values. - The result is then taken modulo 7, which gives a value between 0 and 6, corresponding to the days of the week: 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
Time Complexity: O(1)
AuxiliarySpace: O(1)