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:
C++
#include <iostream>
using namespace std;
void getDate( int d, string m) {
int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string month[] = { "January" , "February" ,
"March" , "April" ,
"May" , "June" ,
"July" , "August" ,
"September" , "October" ,
"November" , "December" };
int cnt = 183;
int cur_month;
for ( int i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
int cur_date = d;
while (1) {
while (cnt > 0 && cur_date <= days[cur_month]) {
cnt -= 1;
cur_date += 1;
}
if (cnt == 0)
break ;
cur_month = (cur_month + 1) % 12;
cur_date = 1;
}
cout << cur_date << " " << month[cur_month] << endl;
}
int main() {
int D = 15;
string M = "January" ;
getDate(D, M);
return 0;
}
|
Java
class GFG{
public static void getDate( int d, String m)
{
int [] days = { 31 , 29 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
String[] month = { "January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December" };
int cnt = 183 ;
int cur_month = 0 ;
for ( int i = 0 ; i < 12 ; i++)
if (m == month[i])
cur_month = i;
int cur_date = d;
while ( true )
{
while (cnt > 0 && cur_date <= days[cur_month])
{
cnt -= 1 ;
cur_date += 1 ;
}
if (cnt == 0 )
break ;
cur_month = (cur_month + 1 ) % 12 ;
cur_date = 1 ;
}
System.out.println(cur_date + " " +
month[cur_month]);
}
public static void main(String args[])
{
int D = 15 ;
String M = "January" ;
getDate(D, M);
}
}
|
Python3
def getDate(d, m):
days = [ 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ]
month = [ 'January' , 'February' ,
'March' , 'April' ,
'May' , 'June' ,
'July' , 'August' ,
'September' , 'October' ,
'November' , 'December' ]
cnt = 183
cur_month = month.index(m)
cur_date = d
while ( 1 ):
while (cnt > 0 and cur_date < = days[cur_month]):
cnt - = 1
cur_date + = 1
if (cnt = = 0 ):
break
cur_month = (cur_month + 1 ) % 12
cur_date = 1
print (cur_date, month[cur_month])
D = 15
M = "January"
getDate(D, M)
|
C#
using System;
class GFG{
static void getDate( int d, string m)
{
int [] days = { 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
string [] month = { "January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December" };
int cnt = 183;
int cur_month = 0;
for ( int i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
int cur_date = d;
while ( true )
{
while (cnt > 0 && cur_date <= days[cur_month])
{
cnt -= 1;
cur_date += 1;
}
if (cnt == 0)
break ;
cur_month = (cur_month + 1) % 12;
cur_date = 1;
}
Console.WriteLine(cur_date + " " +
month[cur_month]);
}
public static void Main()
{
int D = 15;
string M = "January" ;
getDate(D, M);
}
}
|
Javascript
<script>
function getDate(d, m)
{
let days = [ 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 ];
let month = [ "January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December" ];
let cnt = 183;
let cur_month = 0;
for (let i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
let cur_date = d;
while ( true )
{
while (cnt > 0 && cur_date <= days[cur_month])
{
cnt -= 1;
cur_date += 1;
}
if (cnt == 0)
break ;
cur_month = (cur_month + 1) % 12;
cur_date = 1;
}
document.write(cur_date + " " +
month[cur_month]);
}
let D = 15;
let M = "January" ;
getDate(D, M);
</script>
|
Time Complexity: O(1) Auxiliary Space: O(1)
|