FastPrepFastPrep
Problem Brief

Days Between

FULLTIMEOA

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.

Function Description

Complete the function daysBetween in the editor below.

daysBetween has the following parameters:

  • int year1: the year of the first date
  • int month1: the month of the first date
  • int day1: the day of the first date
  • int year2: the year of the second date
  • int month2: the month of the second date
  • int day2: the day of the second date

Returns

int: the number of days between the two dates

1Example 1

Input
year1 = 2010, month1 = 5, day1 = 1, year2 = 2011, month2 = 5, day2 = 1
Output
365
Explanation
From 2010-05-01 to 2011-05-01 there are 365 days.

2Example 2

Input
year1 = 2020, month1 = 2, day1 = 27, year2 = 2020, month2 = 3, day2 = 1
Output
3
Explanation
The interval crosses leap day in 2020: Feb 27 -> Feb 28 -> Feb 29 -> Mar 1, so the difference is 3 days.

3Example 3

Input
year1 = 2019, month1 = 12, day1 = 31, year2 = 2020, month2 = 1, day2 = 1
Output
1
Explanation
The dates are consecutive calendar days across a year boundary.

Constraints

Limits and guarantees your solution can rely on.

  • 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.
public int daysBetween(int year1, int month1, int day1, int year2, int month2, int day2) {
  // write your code here
}
Input

year1

2010

month1

5

day1

1

year2

2011

month2

5

day2

1

Output

365

Sign in to submit your solution.