Given a positive integer D and a string M representing the day and the month of a leap year, the task is to find the date after the next half year.
Examples:
Input: D = 15, M = "January"
Output: 16 July
Explanation: The date from the 15th of January to the next half year is 16th of July.
Input: D = 10, M = "October"
Output: 10 April
Approach: Since a leap year contains 366 days, the given problem can be solved by finding the data after incrementing the current date by 183 days. Follow the steps to solve the problem:
- Store the number of days for each month in that array, say days[].
- Initialize a variable, say curMonth as M, to store the index of the current month.
- Initialize a variable, say curDate as D, to store the current date.
- Initialize a variable, say count as 183, representing the count of days to increment.
- Iterate until the value of count is positive and perform the following steps:
- If the value of count is positive and curDate is at most number of days in the curMonth then decrement the value of count by 1 and increment the value of curDate by 1.
- If the value of count is 0 then break out of the loop.
- Update the value of curMonth by (curMonth + 1)%12.
- After completing the above steps, print the date corresponding to curDate and curMonth as the result.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)