Day of the Week
EasyPrompt
Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day, month and year respectively.
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
Example 1:
Input: day = 31, month = 8, year = 2019
Output: "Saturday"Example 2:
Input: day = 18, month = 7, year = 1999
Output: "Sunday"Example 3:
Input: day = 15, month = 8, year = 1993
Output: "Sunday"
Constraints:
- The given dates are valid dates between the years
1971and2100.
Approaches
3 approaches with complexity analysis and trade-offs.
This approach involves selecting a known date and its corresponding day of the week as a reference point. We then calculate the total number of days that have passed between this reference date and the target date. The day of the week for the target date can be determined by taking this total number of days, adding the reference day's index, and finding the result modulo 7.
Algorithm
-
- Define an array for the days of the week and an array for the number of days in each month.
-
- Initialize a counter
totalDaysto 0.
- Initialize a counter
-
- Iterate from the reference year (1971) to
year - 1. In each iteration, add 365 or 366 tototalDaysbased on whether the year is a leap year.
- Iterate from the reference year (1971) to
-
- Iterate from month 1 to
month - 1. In each iteration, add the number of days in that month tototalDays. Account for leap years for February.
- Iterate from month 1 to
-
- Add the given
daytototalDays.
- Add the given
-
- Calculate the final day index using the total days and the reference day (Friday for Jan 1, 1971). The formula is
(totalDays + 4) % 7.
- Calculate the final day index using the total days and the reference day (Friday for Jan 1, 1971). The formula is
-
- Return the day of the week from the array using the calculated index.
Walkthrough
We'll use January 1st, 1971, as our reference date. A quick search reveals this day was a Friday. We can represent the days of the week with numbers, for example, Sunday=0, Monday=1, ..., Friday=5, Saturday=6. The algorithm proceeds as follows:
- Calculate the total number of days from the reference year (1971) up to the year just before the input
year. This involves iterating through each year and adding 365 days for a common year and 366 for a leap year. - Calculate the total number of days in the months preceding the input
monthwithin the inputyear. We'll use a pre-filled array for the number of days in each month, and we must remember to adjust for February in a leap year. - Add the input
dayto the total count. - Since our reference is Jan 1st, 1971, we have counted the days from Jan 1st, 1971, to the given date. The total number of days elapsed is
total days - 1. - The final day of the week is
(total elapsed days + reference day index) % 7. Since Jan 1, 1971 was a Friday (index 5), the formula is(total_days_from_1971_start + 5 - 1) % 7which simplifies to(totalDays + 4) % 7. A year is a leap year if it is divisible by 4, except for years divisible by 100 but not by 400. The rule is:(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0).
class Solution { public String dayOfTheWeek(int day, int month, int year) { String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Reference date: Jan 1, 1971 was a Friday. int totalDays = 0; // 1. Count days for full years passed since 1971 for (int y = 1971; y < year; y++) { totalDays += isLeapYear(y) ? 366 : 365; } // 2. Count days for full months passed in the current year for (int m = 1; m < month; m++) { if (m == 2 && isLeapYear(year)) { totalDays += 29; } else { totalDays += daysInMonth[m]; } } // 3. Count days in the current month totalDays += day; // Jan 1, 1971 was a Friday. Our daysOfWeek array starts with Sunday. // The index of Friday is 5. // The number of days passed since Jan 1, 1971 is `totalDays - 1`. // The index of the day is ( (days passed) + (index of ref day) ) % 7 // index = ( (totalDays - 1) + 5 ) % 7 = (totalDays + 4) % 7 return daysOfWeek[(totalDays + 4) % 7]; } private boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }}Complexity
Time
O(Y + M), where Y is the number of years between the given year and the reference year (1971), and M is the number of months. Given the constraints (1971-2100), the number of iterations is small and bounded, so it can be considered O(1). However, compared to other approaches, it's less efficient as it involves loops.
Space
O(1), as we only use a few variables and constant-size arrays to store data.
Trade-offs
Pros
Intuitive and easy to understand the logic.
Doesn't rely on complex mathematical formulas.
Demonstrates handling of dates and leap years from first principles.
Cons
More code to write compared to other methods.
Prone to off-by-one errors in counting days or handling the reference date.
Less efficient than a direct formula if the year range were large.
Solutions
Solution
import java.util.Calendar ; class Solution { private static final String [] WEEK = { "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" }; public static String dayOfTheWeek ( int day , int month , int year ) { Calendar calendar = Calendar . getInstance (); calendar . set ( year , month - 1 , day ); return WEEK [ calendar . get ( Calendar . DAY_OF_WEEK ) - 1 ]; } }Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.