![]() |
VOOZH | about |
Given a start year and end year. Write a program to count number of months between given two years (both inclusive).
Examples:
Input: startYear = 1991, endYear = 2023
Output: 396
Explanation: The number of years between 1991 and 2023 (both inclusive) are: 33. So, total number of months = 396.Input: startYear = 2010, endYear = 2023
Output: 168
Explanation: The number of years between 2010 and 2023 (both inclusive) are: 14. So total number of months are: 168.
Approach: To solve the problem, follow the below idea:
We know 1 year has 12 months, so (endYear - startYear + 1) gives number of years between the two given years, where both the starting and ending years are included. So, to find months multiply by 12 as,
1 year = 12 months
n years = n*12 months
Below is the implementation of the above approach:
Number of months between 1991 and 2023(both inclusive) is: 396 months.
Time Complexity: O(1)
Auxiliary space: O(1)