Open In App

# Find the date after next half year from a given date

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the date``// after the next half-year``void` `getDate(``int` `d, string m) {` `    ``// Stores the number of days in the``    ``// months of a leap year``    ``int` `days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};` `    ``// List of months``    ``string month[] = {``"January"``, ``"February"``,``             ``"March"``, ``"April"``,``             ``"May"``, ``"June"``,``             ``"July"``, ``"August"``,``             ``"September"``, ``"October"``,``             ``"November"``, ``"December"``};` `    ``// Days in half of a year``    ``int` `cnt = 183;` `    ``// Index of current month``    ``int` `cur_month;``      ``for``(``int` `i = 0; i < 12; i++)``      ``if``(m == month[i])``          ``cur_month = i;` `    ``// Starting day``    ``int` `cur_date = d;` `    ``while``(1) {` `        ``while``(cnt > 0 && cur_date <= days[cur_month]) {` `            ``// Decrement the value of``            ``// cnt by 1``            ``cnt -= 1;` `            ``// Increment cur_date``            ``cur_date += 1;``        ``}` `        ``// If cnt is equal to 0, then``        ``// break out of the loop``        ``if``(cnt == 0)``            ``break``;` `        ``// Update cur_month``        ``cur_month = (cur_month + 1) % 12;` `        ``// Update cur_date``        ``cur_date = 1;``    ``}` `    ``// Print the resultant date``    ``cout << cur_date << ``" "` `<< month[cur_month] << endl;``}` `// Driver Code``int` `main() {` `    ``int` `D = 15;``    ``string M = ``"January"``;` `    ``// Function Call``    ``getDate(D, M);``      ` `    ``return` `0;``}` `// This code is contributed by Dharanendra L V.`

## Java

 `// Java program for the above approach``class` `GFG{``    ` `// Function to find the date``// after the next half-year``public` `static` `void` `getDate(``int` `d, String m)``{``    ` `    ``// Stores the number of days in the``    ``// months of a leap year``    ``int``[] days = { ``31``, ``29``, ``31``, ``30``, ``31``, ``30``,``                   ``31``, ``31``, ``30``, ``31``, ``30``, ``31` `};` `    ``// List of months``    ``String[] month = { ``"January"``, ``"February"``, ``"March"``,``                       ``"April"``, ``"May"``, ``"June"``, ``"July"``,``                       ``"August"``, ``"September"``, ``"October"``,``                       ``"November"``, ``"December"` `};` `    ``// Days in half of a year``    ``int` `cnt = ``183``;` `    ``// Index of current month``    ``int` `cur_month = ``0``;``    ``for``(``int` `i = ``0``; i < ``12``; i++)``        ``if` `(m == month[i])``            ``cur_month = i;` `    ``// Starting day``    ``int` `cur_date = d;` `    ``while` `(``true``)``    ``{``        ``while` `(cnt > ``0` `&& cur_date <= days[cur_month])``        ``{``            ` `            ``// Decrement the value of``            ``// cnt by 1``            ``cnt -= ``1``;` `            ``// Increment cur_date``            ``cur_date += ``1``;``        ``}` `        ``// If cnt is equal to 0, then``        ``// break out of the loop``        ``if` `(cnt == ``0``)``            ``break``;` `        ``// Update cur_month``        ``cur_month = (cur_month + ``1``) % ``12``;` `        ``// Update cur_date``        ``cur_date = ``1``;``    ``}` `    ``// Print the resultant date``    ``System.out.println(cur_date + ``" "` `+``                       ``month[cur_month]);``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ``int` `D = ``15``;``    ``String M = ``"January"``;` `    ``// Function Call``    ``getDate(D, M);``}``}` `// This code is contributed by SoumikMondal`

## Python3

 `# Python program for the above approach` `# Function to find the date``# after the next half-year``def` `getDate(d, m):` `    ``# Stores the number of days in the``    ``# months of a leap year``    ``days ``=` `[``31``, ``29``, ``31``, ``30``, ``31``, ``30``, ``31``, ``31``, ``30``, ``31``, ``30``, ``31``]` `    ``# List of months``    ``month ``=` `[``'January'``, ``'February'``,``             ``'March'``, ``'April'``,``             ``'May'``, ``'June'``,``             ``'July'``, ``'August'``,``             ``'September'``, ``'October'``,``             ``'November'``, ``'December'``]` `    ``# Days in half of a year``    ``cnt ``=` `183` `    ``# Index of current month``    ``cur_month ``=` `month.index(m)` `    ``# Starting day``    ``cur_date ``=` `d` `    ``while``(``1``):` `        ``while``(cnt > ``0` `and` `cur_date <``=` `days[cur_month]):` `            ``# Decrement the value of``            ``# cnt by 1``            ``cnt ``-``=` `1` `            ``# Increment cur_date``            ``cur_date ``+``=` `1` `        ``# If cnt is equal to 0, then``        ``# break out of the loop``        ``if``(cnt ``=``=` `0``):``            ``break` `        ``# Update cur_month``        ``cur_month ``=` `(cur_month ``+` `1``) ``%` `12` `        ``# Update cur_date``        ``cur_date ``=` `1` `    ``# Print the resultant date``    ``print``(cur_date, month[cur_month])`  `# Driver Code``D ``=` `15``M ``=` `"January"` `# Function Call``getDate(D, M)`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to find the date``// after the next half-year``static` `void` `getDate(``int` `d, ``string` `m)``{``    ` `    ``// Stores the number of days in the``    ``// months of a leap year``    ``int``[] days = { 31, 29, 31, 30, 31, 30,``                   ``31, 31, 30, 31, 30, 31 };` `    ``// List of months``    ``string``[] month = { ``"January"``, ``"February"``, ``"March"``,``                       ``"April"``, ``"May"``, ``"June"``, ``"July"``,``                       ``"August"``, ``"September"``, ``"October"``,``                       ``"November"``, ``"December"` `};` `    ``// Days in half of a year``    ``int` `cnt = 183;` `    ``// Index of current month``    ``int` `cur_month = 0;``    ``for``(``int` `i = 0; i < 12; i++)``        ``if` `(m == month[i])``            ``cur_month = i;` `    ``// Starting day``    ``int` `cur_date = d;` `    ``while` `(``true``)``    ``{``        ``while` `(cnt > 0 && cur_date <= days[cur_month])``        ``{``            ` `            ``// Decrement the value of``            ``// cnt by 1``            ``cnt -= 1;` `            ``// Increment cur_date``            ``cur_date += 1;``        ``}` `        ``// If cnt is equal to 0, then``        ``// break out of the loop``        ``if` `(cnt == 0)``            ``break``;` `        ``// Update cur_month``        ``cur_month = (cur_month + 1) % 12;` `        ``// Update cur_date``        ``cur_date = 1;``    ``}` `    ``// Print the resultant date``    ``Console.WriteLine(cur_date + ``" "` `+``                      ``month[cur_month]);``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `D = 15;``    ``string` `M = ``"January"``;` `    ``// Function Call``    ``getDate(D, M);``}``}` `// This code is contributed by ukasp`

## Javascript

 ``

Output:

`16 July`

Time Complexity: O(1)
Auxiliary Space: O(1)