#1185
Easy Algorithms Day of the week
Math
59.0% acceptance
Feb 25, 2026
452
2559
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"}.
Note: January 1, 1971 was a Friday.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn day_of_the_week(day: i32, month: i32, year: i32) -> String {
// Zeller's congruence or day count from known reference
// January 1, 1971 was Friday (index 4: Sun=0,Mon=1,...Fri=5,Sat=6)
let days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
// Count total days from 1971-01-01
let is_leap = |y: i32| (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
let month_days = [0i32, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let mut total = 0i32;
for y in 1971..year {
total += if is_leap(y) { 366 } else { 365 };
}
for m in 1..month {
total += month_days[m as usize];
if m == 2 && is_leap(year) { total += 1; }
}
total += day - 1;
// 1971-01-01 is Friday = index 5
let idx = (5 + total).rem_euclid(7) as usize;
days[idx].to_string()
}
}