Easy 0 solved 0% acceptance

Days in a Month

Return how many days a month has, including leap years.

What you'll learn

  • Map months to day counts
  • Reuse a leap-year rule
  • Split logic into small functions

Problem

Return the number of days in month of year in the Gregorian calendar. February has 29 days in a leap year: years divisible by 4, except those divisible by 100 unless also divisible by 400. A helper function is_leap(year) keeps the code tidy.

Implement days_in_month(year, month).

Input

year: The year. month: The month, 1 (January) to 12 (December).

Output

Return the number of days.

Constraints

1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12

Examples

Example 1
Input:
year = 2024
month = 2

Expected output:
expected = 29
Input:
long long year = 2024;
long long month = 2;

Expected output:
long long expected = 29;
Input:
long year = 2024;
long month = 2;

Expected output:
long expected = 29;

Explanation

2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.

Example 2
Input:
year = 1900
month = 2

Expected output:
expected = 28
Input:
long long year = 1900;
long long month = 2;

Expected output:
long long expected = 28;
Input:
long year = 1900;
long month = 2;

Expected output:
long expected = 28;

Explanation

1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Most months have a fixed day count; February depends on whether the year is leap.

Hint 2

Use a small lookup for month lengths, then adjust February with the leap-year rule.

Hint 3

Be careful with century years: divisible by 100 is not enough unless also divisible by 400.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.