Hotel Bytelandia Solution

Problem

A number of guests have made reservations. Each reservation consists of an arrival time, and a departure time. The hotel management has hired you to calculate the maximum number of guests that will be at the hotel simultaneously. Note that if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously.

CodeChef Problem Link

Input

Input will begin with an integer T, the number of test cases. Each test case begins with an integer N, the number of guests. Two lines follow, each with exactly N positive integers. The i-th integer of the first line is the arrival time of the i-th guest, and the i-th integer of the second line is the departure time of the i-th guest (which will be strictly greater than the arrival time).

Output

For each test case, print the maximum number of guests that are simultaneously at the hotel.

Constraints
  • T≤100
  • N≤100
  • All arrival/departure times will be between 1 and 1000, inclusive
Sample Input
1
3
1 2 3
4 5 6
Sample Output
3
Solution

Code Implementation

//
//  main.cpp
//  Hotel Bytelandia
//
//  Created by Himanshu on 15/02/22.
//

#include <iostream>
using namespace std;

int main() {
    int T, ans, maxNum, n, maxDepart;
    cin>>T;
    while (T > 0) {
        cin>>n;
        
        maxDepart=0;
        ans = 0;
        
        int *a = new int[n+1]();
        int *b = new int[n+1]();
        
        for (int i=0; i<n; i++) {
            cin>>a[i];
        }
        for (int i=0; i<n; i++) {
            cin>>b[i];
        }
        
        for (int i=0; i<n; i++) {
            if (b[i] > maxDepart) {
                maxDepart = b[i];
            }
        }
        
       //Finding the maximum number of guests present at all time
       //between 2 and maximum possible departure time
        for (int i=2; i<maxDepart; i++) {
            maxNum=0;
            for (int j=0; j<n; j++) {
                if ((a[j] <= i) && (b[j] > i)) {
                    maxNum++;
                }
            }
            if (ans < maxNum) {
                ans = maxNum;
            }
        }
        
        cout<<ans<<endl;
        T--;
    }

    return 0;
}

Time Complexity: O(N*k) (N is the number of guests. k is the maximum departure time.)

Leave a Reply

Your email address will not be published. Required fields are marked *