Skip to main content

Number of Days in a Month - Solution & Explanation

EasyPremiumFree on FleetCodeMath7 min readAsked at: Amazon
Practice this problem

Problem Statement

Given a year year and a month month, return the number of days of that month.

 

Example 1:

Input: year = 1992, month = 7
Output: 31

Example 2:

Input: year = 2000, month = 2
Output: 29

Example 3:

Input: year = 1900, month = 2
Output: 28

 

Constraints:

  • 1583 <= year <= 2100
  • 1 <= month <= 12

Approach Overview

Problem Overview: You are given a year and a month. Return how many days exist in that month for the given year. The only tricky case is February because its length depends on whether the year is a leap year.

The problem sits in the math category and mainly tests whether you remember the leap year rules and can map months to their correct number of days.

Approach 1: Month Table + Leap Year Check (O(1) time, O(1) space)

Store the number of days for each month in a fixed array such as [31,28,31,30,31,30,31,31,30,31,30,31]. For most months, simply return the value from the table using month - 1 as the index. February requires special handling. If the month is 2, determine whether the given year is a leap year and return 29 instead of 28.

The leap year rule follows the Gregorian calendar: a year is a leap year if it is divisible by 4, except years divisible by 100 are not leap years unless they are also divisible by 400. In code, this usually becomes:

(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

After computing this condition, adjust February’s value accordingly. All other months return the precomputed value directly from the array. The algorithm performs only a few arithmetic checks and one array lookup, giving constant time and space complexity.

This approach is essentially a small implementation task backed by a simple math rule. No iteration, recursion, or extra data structures are required.

Approach 2: Conditional Month Groups (O(1) time, O(1) space)

Another way is to group months by the number of days instead of using an array. Months with 31 days are {1,3,5,7,8,10,12}. Months with 30 days are {4,6,9,11}. February is handled separately using the same leap year rule.

The logic becomes a sequence of conditional checks: if the month belongs to the 31-day set, return 31; if it belongs to the 30-day set, return 30; otherwise handle February with the leap year formula. This version avoids arrays but expresses the same idea through conditionals.

Recommended for interviews: The month-table approach is usually the cleanest and most readable. Interviewers expect you to know the leap year rule and apply it correctly. Writing the simple array plus the leap-year check demonstrates solid attention to edge cases while keeping the solution O(1) in both time and space.

Solution

We can first determine whether the given year is a leap year. If the year can be divided by 4 but not by 100, or can be divided by 400, then this year is a leap year.

February has 29 days in a leap year and 28 days in a common year.

We can use an array days to store the number of days in each month of the current year, where days[0]=0, days[i] represents the number of days in the ith month of the current year. Then the answer is days[month].

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Month Table + Leap Year CheckO(1)O(1)Best general solution. Simple array lookup with a leap year adjustment for February.
Conditional Month GroupsO(1)O(1)Useful if you prefer explicit conditional logic instead of maintaining a lookup table.

Video Solution

Leetcode 1118. Number of Days in a Month Python Solution • TechZoo • 199 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Number of Days in a Month easy or hard?
Number of Days in a Month is classified as an Easy problem. The main challenge is remembering and correctly implementing the leap year rule while mapping months to their standard day counts.
Number of Days in a Month Python/Java solution
In Python or Java, define an array with month lengths and check if the month equals 2. If it is February, apply the leap year formula using modulo operations. Otherwise return the value directly from the array. The implementation remains O(1) time and space.
How to solve Number of Days in a Month in O(1)?
Use a precomputed array containing the number of days for each month. If the input month is February, evaluate the leap year condition: divisible by 4 but not by 100 unless also divisible by 400. Return 29 for leap years, otherwise 28.
What is the best approach for Number of Days in a Month?
The most common solution uses a fixed array for month lengths and a leap year check for February. You store the days for each month and adjust February to 29 if the year satisfies the leap year rule. This runs in O(1) time and O(1) space.
Is Number of Days in a Month asked at Google/Amazon/Meta?
The problem itself is more of a basic logic and calendar math question, typically seen in coding platforms like LeetCode for practice. Similar logic questions about date handling or leap year calculations can appear in interviews at large tech companies.
What data structure is used in Number of Days in a Month?
Most implementations use a simple array or list storing the days for each month. Aside from that, the solution mainly relies on arithmetic operations to evaluate the leap year rule.
What is the time complexity of Number of Days in a Month?
The optimal solution runs in O(1) time because it performs only a few arithmetic checks and a constant-time array lookup. Space complexity is also O(1) since only a small fixed array or a few variables are used.

Ready to solve this problem?

Practice Number of Days in a Month with our built-in code editor and test cases.

Practice on FleetCode