Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
660 views

in Interview-Experiences by Expert (2,200 points) | 660 views

1 Answer

0 like 0 dislike
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int earliestMaxTrafficTime(vector<int>& start, vector<int>& end) {
    vector<pair<int, int>> events;
    
    // Create events for each client interaction start and end
    for (int i = 0; i < start.size(); ++i) {
        events.push_back({start[i], 1}); // Client joins
        events.push_back({end[i], -1});  // Client leaves
    }
    
    // Sort events based on time, and if time is the same, join events come first
    sort(events.begin(), events.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
        if (a.first == b.first)
            return a.second > b.second; // Join events first
        return a.first < b.first;
    });
    
    int maxTraffic = 0;
    int maxTrafficTime = 0;
    int currentTraffic = 0;
    
    // Iterate through events to find maximum traffic time
    for (auto& event : events) {
        currentTraffic += event.second;
        if (currentTraffic > maxTraffic) {
            maxTraffic = currentTraffic;
            maxTrafficTime = event.first;
        }
    }
    
    return maxTrafficTime;
}

int main() {
    vector<int> start = {2,3,7,4,7};
    vector<int> end = {4,5,8,7,10};

    cout << "Earliest time with maximum traffic: " << earliestMaxTrafficTime(start, end) << endl;

    return 0;
}
by (140 points)