Activity selection problem


please tell me whaats wrong?

Hi @Shreya-Gupta-2383169445069382,
in ur code, 1)Put task class on top.
2)use i-- instead of i++ in for loop
for(int i=n-2;i>=0;i–)
{
if(a[i].e<=a[i+1].s)
{a[i].no=(a[i+1].no )+ 1;}
else
a[i].no=(a[i+1].no );
}
The above will remove segmentation fault.
also try modifying ur logic. start finding from 0 to n-1 instead of starting from end.
if input array={(1,2),(3,4),(1,5)}, ans should be 2 but ur code give 1.
HINT. SORT ARRAY BASED ON ENDING TIME.
if u still find any problem, u can post it here.

can u please explain why should we SORT ARRAY BASED ON ENDING TIME?

sorting wrt end time as we need to follow greedy approach. Since, we need to maximise number of activity, we prefer to take one which end first rather than start first.
if input array={(2,3),(4,5),(1,6)}; if we sort based on start, ans=1 but if in end time, ans =2.
Hope dis helps

how about sorting acc. to the time taken by the task? endtime-starttime?

@Shreya-Gupta-2383169445069382, I dont think, that would work.
eg. input={(1,5),(4,7),(6,10)}
here,using time taken, (4,7) will be taken only.
but we can take (1,5),(6,10).
Hope dis helps.

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.