# What algorithm should be used to find the closest set of dates?

I have tried to outline my problem as structured as possible, here is a rough overview, I am trying to find the best matching stay for a hotel booking system. If someone inputs check in and checkout out I want to find the closest availability to that date available. Only caveat being checkin and checkout are only allowed on specific days.

Structured problem:

Given availability data for a hotel room provided as an array of objects where each item is a single day the structure is as follows:

{ day: Date, available: boolean, checkInAllowed: boolean, checkoutAllowed: boolean }


example:

[
{ day: 12-01-2025, available: true, checkInAllowed: true, checkoutAllowed: true },
{ day: 13-01-2025, available: true, checkInAllowed: false, checkoutAllowed: false },
{ day: 14-01-2025, available: false, checkInAllowed: true, checkoutAllowed: true }
]


When an input of requestCheckinDate: Date, and requestedCheckoutDate is provided

Then the closest matching available stay should be returned. “Closest matching” should take into account the distance from original request dates and the difference between length of stay.

What I have attempted so far?

I have tried using a binary search to find the best match based but find looping through the data and finding available "segments" and then comparing these segments to the original inputed checkin and checkout, although it can find a good match in terms of duration of stay, it doesn't do a good job of finding the closest one to the original dates.

Previous to me trying to re build this project the code just generated a bunch of similar dates and checked if they were available, I.e move checkin and checkout -1 day or + 1 day etc. Which is a really bad solution but did actually kind of work.

I'm trying to find the most efficiency solution as I am hoping for it to return this data to the customer very quickly. Any ideas?

• Please define how you measure "best matching". What utility function are you seekin gto maximize?
– D.W.
Commented May 11 at 22:41
• I suppose it's slightly hard to define. But as an example. If someone asks for Monday 12th to Friday 16th only availble options are Tuesday -> Friday or Sunday to Thursday, it would pick the later. Commented May 11 at 23:35
• I would define it as the least amount of changes, a change being either: Shifting the dates by N number of days, Changing the length of stay by N number of days. The result should find with lowest total N, if this makes sense? Commented May 11 at 23:37
• I'm a little lost. What is N and how does it affect the number of changes? Is N an input? If I shift both the start and end date, is that 2 changes or 1 change? Please edit the question to clarify the problem statement, rather than putting clarifications in the comments.
– D.W.
Commented May 12 at 6:20