Help me in solving HOTEL problem

My issue

My code

#include <bits/stdc++.h>
using namespace std;

bool compare(pair<int,int>p1,pair<int,int>p2)
{
    return p1.second<p2.second;
}

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int N,maxi=INT_MIN,cnt=0;
	    cin>>N;
	    vector<int>arrival(N,0);
	    for(int i=0;i<N;i++)
	    {
	        cin>>arrival[i];
	    }
	    vector<int>departure(N,0);
	    for(int j=0;j<N;j++)
	    {
	        cin>>departure[j];
	    }
	    vector<pair<int,int>>v;
	    for(int i=0;i<N;i++)
	    {
	        v.push_back(make_pair(arrival[i],departure[i]));
	    }
	    sort(v.begin(),v.end(),compare);
	    for(int i=0;i<N;i++)
	    {
	        cnt=1;
	        for(int j=0;j<N;j++)
	        {
	            if(i!=j)
	            {
	                
    	            if(v[i].second>v[j].first && v[i].first<v[j].second)
    	            {
    	                cnt+=1;
    	                //cout<<v[j].first<<" ";
    	            }
    	        }
    	        maxi=max(maxi,cnt);
	       }
	       //cout<<endl;
	    }
	    cout<<maxi<<endl;
	}
	return 0;
}

Problem Link: HOTEL Problem - CodeChef

@kumarhemendra7
Your conditions is not right for a range to intersect there are three possibilities left intersect, right intersect, and completely lies inside . So u have to mention all three.
There is one more freq logic that i have used in my code this is much easier to implement .
Hope u will get it.

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    int a[n],b[n];
	    int freq[1001]={0};
	    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++)
	    {
	        for(int j=a[i];j<b[i];j++)
	        {
	            freq[j]++;
	        }
	    }
	    int mx=0;
	    for(int i=0;i<=1000;i++)
	    {
	        mx=max(mx,freq[i]);
	    }
	    cout<<mx<<endl;
	}
	return 0;
}
1 Like