Maximum Intervals Overlap | Practice | GeeksforGeeks

Consider a big party where N guests came to it and a log register for guest’s entry and exit times was maintained. Find the minimum time at which there were maximum guests at the party. Note that entries in the register are not in any order.
Note: Guests are leaving after the exit times.
Example 1:
Input:
N = 5
Entry= {1, 2,10, 5, 5}
Exit = {4, 5, 12, 9, 12}
Output: 3 5
Explanation: At time 5 there were 
             guest number 2, 4 and 5 present.
Example 2:
Input:
N = 7
Entry= {13, 28, 29, 14, 40, 17, 3}
Exit = {107, 95, 111, 105, 70, 127, 74}
Output: 7 40
Explanation: At time 40 there were 
             all 7 guests present in the party.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMaxGuests() which takes 3 arguments(Entry array, Exit array, N) and returns the maximum number of guests present at a particular time and that time as a pair.
Expected Time Complexity: O(N*Log(N) ).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 105
1 <= Entry[i],Exit[i] <= 105
+1
sumeethmoolyaa2 months ago
class Solution{public int[] findMaxGuests(int[] Entry,int Exit[], int N){       int[][] entries = new int[N][2];              for(int i=0; i < N; i++){           entries[i][0] = Entry[i];           entries[i][1] = Exit[i];       }              int maximum = -1;                            for(int[] log : entries){           maximum = Math.max(maximum, log[1]);       }                   int register[] = new int[maximum+1];       int count = 1;              for(int i=0; i < N; i++){           int entry = entries[i][0], exit = entries[i][1];                      while(entry <= exit){               register[entry] += count;               entry++;           }       }              // for(int i=0; i < register.length; i++){       //     System.out.print(register[i] + " ");       // }              maximum = -1;       int index = -1;       for(int i=0; i < register.length; i++){           if(register[i] > maximum){               maximum = register[i];               index = i;           }       }              int[] result = new int[2];       result[0] = maximum;       result[1] = index;              return result;       }}
+1
anshulpro273 months ago
{public int[] findMaxGuests(int[] a,int b[], int n){       Arrays.sort(a);       Arrays.sort(b);       int ans[]={0,0};       int cnt=0,i=0,j=0;       while(i<n && j<n)       {           if(a[i]<=b[j])           {              cnt++;              if(ans[0]<cnt)              {                  ans[0]=cnt;                  ans[1]=a[i];              }              i++;           }        else{            cnt--;            j++;         }       }       return ans;}}
0
anutiger6 months ago
	vector<int> findMaxGuests(int Entry[], int Exit[], int N)
	{
	   // Your code goes here
	   sort(Entry,Entry + N);
	   sort(Exit,Exit + N);
	    int i  = 0;
	    int j = 0;
	    int st = INT_MIN;
	    vector<int> res;
	    int ret = INT_MIN;
	    while(i < N){
	        if(Entry[i] <= Exit[j]){
	            st  = max(st,Entry[i]);
	            i++;
	        }
	        else if(Entry[i] > Exit[j]){
	            if(ret < i - j){
	                res = {i - j,st};
	                ret = i - j;
	            }
	            while(Exit[j] < Entry[i]){
	                j++;
	            }
	        }
	    }
	    if(ret < i - j){
	        res = {i - j,st};
	    }
	    return res;
	}
+1
sknwd88647 months ago
vector<int> findMaxGuests(int arr[], int dep[], int n)
	{
	    sort(arr,arr+n);
	    sort(dep,dep+n);
	    
	    int i=1;
	    int j=0;
	    int res=1;
	    int curr=1;
	    
	    int mintime=arr[0];
	     
	    while(i<n && j<n)
	    {
	        if(arr[i]<=dep[j])
	        {
	            curr++;
	            i++;
	        }
	        else
	        {
	            curr--;
	            j++;
	        }
	        if(curr>res)
	        {
	            res=curr;
	            mintime=arr[i-1];
	        }
	    }
	    
	    vector<int> ans={res,mintime};
	    return ans;
	}
0
ARPIT PRASAD9 months ago
ARPIT PRASAD
Another problem but same type
0
Mukul goyal10 months ago
Mukul goyal
Donot try to merge the entry times and exit times and then sort, you will get TLE.
0
Pawan Chaturvedi11 months ago
Pawan Chaturvedi
https://uploads.disquscdn.c...
+2
Nihal Chaturvedi1 year ago
Nihal Chaturvedi
https://uploads.disquscdn.c... Whenever You are given with Entry Time and Exit time of some items and asked to Find  the maximum items at a same time . Sort the Entry and Exit separately and then find the max .
0
SURAJ KUMAR1 year ago
SURAJ KUMAR
https://youtu.be/vzOiwd9oiHo
0
ABHISHEK Tiwari1 year ago
ABHISHEK Tiwari
Execution Time : 0.49
struct Comp {  bool operator()(const std::pair<int, int=""> &a, const std::pair<int, int=""> &b) {    if (a.first != b.first) {      return a.first < b.first;    }    return a.second > b.second;  }};class Solution{public:vector<int> findMaxGuests(int Entry[], int Exit[], int N){   // Your code goes here       vector<pair<int,int>> v;       vector<int> ans(2,0);              for(int i=0;i<n;i++) {="" v.push_back({exit[i],="" -1});="" v.push_back({entry[i],="" 1});="" }="" comp="" comp_functor;="" sort(v.begin(),="" v.end(),="" comp_functor);="" int="" res="0;" for(auto="" x:v)="" {="" res="" +="x.second;" if(res=""> ans[0]){               ans[1] = x.first;               ans[0] = res;           }       }              return ans;}};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.