- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find if k bookings possible with given arrival and departure times in C++
In this problem, we are given two arrays consisting of N values denoting arrival and departure at hotel and an integer k. Our task is to find if k bookings are possible with given arrival and departure times.
Problem Description: Here, we need to check if the hotel with k rooms is able to accommodate all arrivals and departures.
Let’s take an example to understand the problem,
Input: Arrivals : {1 4 5 7}
Departures : {3 5 6 9}
K = 1
Output: Yes
Solution approach:
To solve the problem, we will store arrival and departure for the hotel in an auxiliary array with a label that it is for arrival or departure. Then sort this array and count the number of active bookings for the hotel.
If arrival, count++
If departure, count--.
If at any point the booking at any time is more than k, return false, else return true.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; bool isBookingValid(int arrival[], int departure[], int n, int k){ vector<pair<int, int> > auxArray; int activeBookings = 0, maxBookings = 0; for (int i = 0; i < n; i++) { auxArray.push_back(make_pair(arrival[i], 1)); auxArray.push_back(make_pair(departure[i], 0)); } sort(auxArray.begin(), auxArray.end()); for (int i = 0; i < auxArray.size(); i++) { if (auxArray[i].second == 1) { activeBookings++; maxBookings = max(maxBookings, activeBookings); } else activeBookings--; } return (k >= maxBookings); } int main(){ int arrival[] = { 1, 4, 5, 7 }; int departure[] = { 3, 5, 6, 9 }; int k = 1; int n = sizeof(arrival) / sizeof(arrival[0]); if(isBookingValid(arrival,departure, n, k)) cout<<"All booking are possible"; else cout<<"Booking not possible"; return 0; }
Output
All booking are possible
Another Approach:
We can eliminate the use of auxiliary array. We can check the bookings of the hotel using the two arrays given for departure and arrivals.
Then check overlapping and if it is greater than k, return false. Else return true.
Since, there are k rooms, an easy approach would be checking kth arrival, and check if it falls in the range.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; bool isBookingPossible(int arrival[], int departure[], int K, int N){ sort(arrival, arrival + N); sort(departure, departure + N); for(int i = 0; i < N; i++) { if (i + K < N && arrival[i + K] < departure[i]) { return false; } } return true; } int main(){ int arrival[] = { 1, 2, 3 }; int departure[] = { 2, 3, 4 }; int N = sizeof(arrival) / sizeof(arrival[0]); int K = 1; if(isBookingPossible(arrival, departure, K, N)) cout<<"All booking are possible"; else cout<<"Booking not possible"; return 0; }
Output
All booking are possible
To Continue Learning Please Login
Login with Google