Find the date after next half year from a given date
Last Updated :
07 Jun, 2021
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 <iostream>
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
<script>
// Javascript program for the above approach
// Function to find the date
// after the next half-year
function getDate(d, m)
{
// Stores the number of days in the
// months of a leap year
let days = [ 31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 ];
// List of months
let month = [ "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" ];
// Days in half of a year
let cnt = 183;
// Index of current month
let cur_month = 0;
for(let i = 0; i < 12; i++)
if (m == month[i])
cur_month = i;
// Starting day
let 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
document.write(cur_date + " " +
month[cur_month]);
}
// Driver Code
let D = 15;
let M = "January";
// Function Call
getDate(D, M);
// This code is contributed by susmitakundugoaldanga
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find day of the week for a given date Given a date (day, month, year), the task is to determine the day of the week on which that date falls. The function should be able to compute the day for any date in the past or future. The function should return values from 0 to 6 where 0 means Sunday, 1 Monday and so on.Examples:Input: d = 30, m
12 min read
Find the day number in the current year for the given date Given string str which represents a date formatted as YYYY-MM-DD, the task is to find the day number for the current year. For example, 1st January is the 1st day of the year, 2nd January is the 2nd day of the year, 1st February is the 32nd day of the year and so on. Examples: Input: str = "2019-01-
6 min read
Find the next identical calendar year You are given an year Y, find the next identical calendar year to Y.Examples : Input : 2017 Output : 2023 Input : 2018 Output : 2029 An year x is identical to a given previous year y if following two conditions are satisfied. x starts with same day as y.If y is leap year, then x is also. If y is not
7 min read
Date after adding given number of days to the given date Given a date and a positive integer x. The task is to find the date after adding x days to the given date Examples: Input : d1 = 14, m1 = 5, y1 = 2017, x = 10Output : d2 = 24, m2 = 5, y2 = 2017 Input : d1 = 14, m1 = 3, y1 = 2015, x = 366Output : d2 = 14, m2 = 3, y2 = 2016 Method 1: 1) Let given date
12 min read
Print calendar for a given year in C++ Prerequisite : Find day of the week for given dateProblem: To print the calendar of any given year. The program should be such that it can prints the calendar of any input year.Implementation: CPP // A C++ Program to Implement a Calendar // of an year #include<bits/stdc++.h> using namespace st
6 min read
Queries to find the future closest date Given an array arr[] consisting of N strings and an array Query[] consisting of Q queries. Each string in arrays arr[] and Query[] is of the form D/M/Y where D, M and Y denotes the date, month and year. For each query, the task is to print the next closest date from the given array arr[]. If no such
12 min read