Test case 0 is not passing

// Activity selection problem

#include
#include
using namespace std;

bool compare(pair<int,int> a,pair<int,int> b)
{
return a.second<b.second;
}

int main() {

int job=1;
int t; //test cases
cin>>t;

while(t>0)
{
int n;

cin>>n;
pair<int,int>act[n];
for(int i=0;i<n;i++)
{

cin>>act[i].first;
cin>>act[i].second;
}

sort(act,act+n,compare);

   int last_ended=act[0].second;

for(int i=1;i<n;i++)
{
if(act[i].first<act[i].second)
{
if(act[i].first>=last_ended)
{

    job++;
    last_ended=act[i].second;
}
}

}

t–;
}

cout<<job<<endl;

return 0;
}

Hey @anandsingh3210 share your code using ide.codingblocks.com so that j can debug and share the link with you.

how to share it i don’t know

previous TAs were sharing their own coding blocks ide

I got your code via submission. Debugging it. Will let you know.

Check now ->

sir i did not understand this logic and where I was wrong

int last_ended=0;//act[0].second;
for(int i=0;i<n;i++)
{
if(act[i].first<last_ended){
continue;
}
else{
job++;
last_ended=act[i].second;
}
}

sir please explain this

Don’t focus on value of i, the wrong answer is because you have declared job outside while t loop, and after execution of this loop 1 time it will contain answer of 1st test case let it be x, then for 2nd test case job will have initial value of x. But it should have been 0. Check now ->

yes I understood the mistake

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.