Problem · Math

Days Between

Learn this problem
EasyOptiver logoOptiverFULLTIMEOA

Problem statement

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

daysBetween(year1: int, month1: int, day1: int, year2: int, month2: int, day2: int) → int

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

Examples

Example 1

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

Example 2

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

Example 3

year1 = 2019month1 = 12day1 = 31year2 = 2020month2 = 1day2 = 1return = 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

drafts saved locally
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
checking account