Number of Days in a Month - Problem
Given a year and a month, return the number of days in that month.
You need to handle leap years correctly. A leap year occurs:
- Every 4 years, BUT
- NOT every 100 years, BUT
- Every 400 years
For example, 2000 and 2004 are leap years, but 1900 is not.
Remember that February has 28 days in regular years and 29 days in leap years.
Input & Output
Example 1 — Leap Year February
$
Input:
year = 2020, month = 2
›
Output:
29
💡 Note:
2020 is a leap year (divisible by 4, not by 100), so February has 29 days
Example 2 — Regular Month
$
Input:
year = 2019, month = 8
›
Output:
31
💡 Note:
August always has 31 days regardless of leap year
Example 3 — Non-Leap February
$
Input:
year = 1900, month = 2
›
Output:
28
💡 Note:
1900 is not a leap year (divisible by 100 but not by 400), so February has 28 days
Constraints
- 1583 ≤ year ≤ 2100
- 1 ≤ month ≤ 12
Visualization
Tap to expand
Understanding the Visualization
1
Input
Year and month values
2
Logic
Check leap year for February, use fixed values for others
3
Output
Number of days in that month
Key Takeaway
🎯 Key Insight: February is the only month affected by leap years - use modulo arithmetic to check leap year conditions efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code