# Find if k bookings possible with given arrival and departure times

A hotel manager has to process N advance bookings of rooms for the next season. His hotel has K rooms. Bookings contain an arrival date and a departure date. He wants to find out whether there are enough rooms in the hotel to satisfy the demand.

The idea is to sort the arrays and keep track of overlaps.

**Examples:**

Input : Arrivals : [1 3 5] Departures : [2 6 8] K: 1 Output: False Hotel manager needs at least two rooms as the second and third intervals overlap.

The idea is store arrival and departure times in an auxiliary array with an additional marker to indicate whether the time is arrival or departure. Now sort the array. Process the sorted array, for every arrival increment active bookings. And for every departure, decrement. Keep track of maximum active bookings. If the count of active bookings at any moment is more than k, then return false. Else return true.

Below is the code for above approach.

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `areBookingsPossible(` `int` `arrival[], ` ` ` `int` `departure[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `vector<pair<` `int` `, ` `int` `> > ans; ` ` ` ` ` `// create a common vector both arrivals ` ` ` `// and departures. ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `ans.push_back(make_pair(arrival[i], 1)); ` ` ` `ans.push_back(make_pair(departure[i], 0)); ` ` ` `} ` ` ` ` ` `// sort the vector ` ` ` `sort(ans.begin(), ans.end()); ` ` ` ` ` `int` `curr_active = 0, max_active = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < ans.size(); i++) { ` ` ` ` ` `// if new arrival, increment current ` ` ` `// guests count and update max active ` ` ` `// guests so far ` ` ` `if` `(ans[i].second == 1) { ` ` ` `curr_active++; ` ` ` `max_active = max(max_active, ` ` ` `curr_active); ` ` ` `} ` ` ` ` ` `// if a guest departs, decrement ` ` ` `// current guests count. ` ` ` `else` ` ` `curr_active--; ` ` ` `} ` ` ` ` ` `// if max active guests at any instant ` ` ` `// were more than the available rooms, ` ` ` `// return false. Else return true. ` ` ` `return` `(k >= max_active); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arrival[] = { 1, 3, 5 }; ` ` ` `int` `departure[] = { 2, 6, 8 }; ` ` ` `int` `n = ` `sizeof` `(arrival) / ` `sizeof` `(arrival[0]); ` ` ` `cout << (areBookingsPossible(arrival, ` ` ` `departure, n, 1) ? ` `"Yes\n"` `: ` `"No\n"` `); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 code for the above approach.

def areBookingsPossible(arrival, departure, n, k):

ans = []

# Create a common vector both arrivals

# and departures.

for i in range(0, n):

ans.append((arrival[i], 1))

ans.append((departure[i], 0))

# Sort the vector

ans.sort()

curr_active, max_active = 0, 0

for i in range(0, len(ans)):

# If new arrival, increment current

# guests count and update max active

# guests so far

if ans[i][1] == 1:

curr_active += 1

max_active = max(max_active,

curr_active)

# if a guest departs, decrement

# current guests count.

else:

curr_active -= 1

# If max active guests at any instant

# were more than the available rooms,

# return false. Else return true.

return k >= max_active

# Driver Code

if __name__ == “__main__”:

arrival = [1, 3, 5]

departure = [2, 6, 8]

n = len(arrival)

if areBookingsPossible(arrival,

departure, n, 1):

print(“Yes”)

else:

print(“No”)

# This code is contributed by Rituraj Jain

**Output:**

No

Time Complexity: O(n Log n)

Auxiliary Space: O(n)

## Recommended Posts:

- Find the only element that appears b times
- Find the K-th minimum element from an array concatenated M times
- Given an array of of size n and a number k, find all elements that appear more than n/k times
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Find the Number Occurring Odd Number of Times
- First element occurring k times in an array
- Sum of all elements repeating 'k' times in an array
- Remove elements that appear strictly less than k times
- Remove elements from the array which appear more than k times
- First element that appears even number of times in an array
- Encrypt a string by repeating i-th character i times
- Sum of array elements after every element x is XORed itself x times
- Two odd occurring elements in an array where all other occur even times
- Largest element in the array that is repeated exactly k times
- Smallest element in an array that is repeated exactly 'k' times.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.