Problem · Math
Days Between
Write a function daysBetween that returns the number of days between two calendar dates.
Each date is represented by three integers: year, month, and day. The first date is guaranteed to occur before the second date.
Do not use system-provided date objects or built-in date-difference helpers. Compute the answer directly from the date components.
Complete the function daysBetween in the editor below.
daysBetween has the following parameters:
int year1: the year of the first dateint month1: the month of the first dateint day1: the day of the first dateint year2: the year of the second dateint month2: the month of the second dateint day2: the day of the second date
Returns
int: the number of days between the two dates
Examples
01 · Example 1
year1 = 2010 month1 = 5 day1 = 1 year2 = 2011 month2 = 5 day2 = 1 return = 365
From
2010-05-01 to 2011-05-01 there are 365 days.02 · Example 2
year1 = 2020 month1 = 2 day1 = 27 year2 = 2020 month2 = 3 day2 = 1 return = 3
The interval crosses leap day in
2020: Feb 27 -> Feb 28 -> Feb 29 -> Mar 1, so the difference is 3 days.03 · Example 3
year1 = 2019 month1 = 12 day1 = 31 year2 = 2020 month2 = 1 day2 = 1 return = 1
The dates are consecutive calendar days across a year boundary.
Constraints
1 ≤ month1, month2 ≤ 12- Both input dates are valid calendar dates.
- The first date always occurs before the second date.
- Do not use built-in date libraries or system date objects.
More Optiver problems
public int daysBetween(int year1, int month1, int day1, int year2, int month2, int day2) {
// write your code here
}
year12010
month15
day11
year22011
month25
day21
expected365
sign in to submit