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
Input : d1 = 14, m1 = 5, y1 = 2017, x = 10
Output : d2 = 24, m2 = 5, y2 = 2017
Input : d1 = 14, m1 = 3, y1 = 2015, x = 366
Output : d2 = 14, m2 = 3, y2 = 2016
1) Let given date be d1, m1 and y1. Find offset (number of days spent from beginning to given date) of given year (Refer offsetDays() below)
2) Let offset found in above step be offset1. Find result year y2 and offset of result year offset2 (Refer highlighted code below)
3) Find days and months from offset2 and y2. (Refer revoffsetDays() below).
Below is the implementation of above steps.
d2 = 14, m2 = 3, y2 = 2016
Time complexity: O(1) as it is performing constant operations
Auxiliary space: O(1)
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...