What's wrong here? Failing the 3rd test case

include<bits/stdc++.h>
using namespace std;
int main(){
int totalMeeting;
cin>>totalMeeting;
vector<pair<int, int>> meetings;
for(int meeting = 0;meeting<totalMeeting;meeting++){
int startTime, endTime;
cin>>startTime>>endTime;
meetings.push_back(make_pair(startTime,endTime));
}

// sort based on start time

sort(meetings.begin(), meetings.end());

int runningMeetingEndtime = meetings[0].second;
for(int i=1;i<totalMeeting;i++){
    int currentStarttime,currentEndtime;
    currentStarttime = meetings[i].first;
    currentEndtime = meetings[i].second;

    if(currentStarttime>runningMeetingEndtime){
        cout<<"false";
        return 0;
    }
    runningMeetingEndtime = currentEndtime;
}
cout<<"true";
return 0;

}

Hello @sauravgupta2800 i have corrected your code check it now.


you should sort it according to ending time.
if you have any doubt you can ask here:
Happy Learning!!

I want to know, why sorting based on starting time will be wrong? Explain me with some test cases.

Hello @sauravgupta2800 i think you will understand with this test case:
3
5 8
9 15
10 11
in this as the data is present in the sorted format accordignt to the starting time but there can be any meeting which will start earlier then the ending time of the any previoud meeting then your code will fail.
Happy Learning!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.