Activity selection prob

what’s wrong in my code?

You need to sort according to ending time, so make this change in your comparator function.
bool cmp(pair<int,int>&a,pair<int,int>&b)
{
return a.second<=b.second;

}
Now you don’t need to run loop for all i’s you can do it in just one iteration because the greedy algorithm whose time complexity is O(nlogn) works. So make this change

  int count=1;
  int maxm=0;
  
  
      int curr=a[0].second;
      for(int j=1;j<n;j++)
      {
          if(curr<=a[j].first){
          count++;
          curr=a[j].second;
          }
	  }
  
  cout<<count<<endl;
1 Like

@rockstarpkm If you don’t have any further queries please mark the doubt as resolved.

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.