Activity Selection Problem

What is the error in my code: https://ide.codingblocks.com/s/482000

hello @rahul_bansal

sort on the basis of ending time,
we want to pick that activity first which finish less in time

I know we have to pick that activity first which finishes in less time but I have sort on the basis of first and by using if condition I pick that starting time which starts after first ends like suppose example:

1
3
10 15
20 35
12 22
15 19

if I sort the above by first then:
10 15
12 22
15 19
20 35

In this k=15 and after that it iterating for >=K then second activity not satisfy this but third satisfy this hence count++ and K=v.second means K=19 then fourth also satisfies count++; Then finally loop ends and output is 3.

What’s wrong in this condition?

@rahul_bansal
consider this case->

1 1000
2 3
3 4
4 5

clearly ur code will produce wrong result for such cases

1 Like

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.

1 Like