Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Thursday, 4 June 2020

Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order.

Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in the register are not in any order.

Here is a simple and easy solution with using vector pair and hashing. Its execution time is 0.01s.

Sentiment, Quote, Sign, Poster
#include <bits/stdc++.h>
using namespace std;

int main() {
	//code
	int t;
	cin>>t;
	while(t--)
	{
	    int n,x,maximum=0;
	    cin>>n;
	   vector< pair<int,int> >mp;
	    vector<int> v;
	    for(int i=0;i<n;++i)
	    {
	        cin>>x;
	        v.push_back(x);

	    }
	    
	     for(int i=0;i<n;++i)
	    {
	        cin>>x;
	        if(x>maximum)
	        maximum=x;
	        mp.push_back(make_pair(v[i],x));
	    }
	    
	    sort(mp.begin(),mp.end());
	    
	    int ar[maximum+1]={0};
	    
	    for(int i=0;i<n;++i)
	    {
	        ar[mp[i].first]+=1;
	        ar[mp[i].second+1]-=1;
	    }
	    
	    int mx=0,tim=0,sum=0;
	    
	    for(int i=mp[0].first;i<=maximum;++i)
	    {
    	        sum+=ar[i];
    	        if(mx<sum)
    	        {
        	       mx=sum;
        	       tim=i;
    	        }
	        
	    }

	    
	    cout<<mx<<" "<<tim<<endl;
	    
	}
	return 0;
}