ACTIVITY SELECTION PROBLEMS

Please, check my code, sample output is same but test cases are not passing.

hi @CODER_JATIN first you need to sort the activities according to ending time, and then you can greedily choose activities to maximise the number of activities.

But , the approach that i’d used is also giving the same output, why test cases are not passing/

@CODER_JATIN the input could be given in any random order, you cant be sure that this approach will always work for any order in which the activities are given. I just changed the order of activities and now see the answer is coming out to be wrong.

input: 
1 
3
20 30
12 15
10 20 

your output : 1
expected output : 2

okay okay , understoood… Thanks.

How can i make a comparator fuction for sorting of pair<int,int> .

https://ide.codingblocks.com/s/303211 , AB CHECK KARNA DI ???

https://ide.codingblocks.com/s/303211 , AB CHECK KARNA DI ???

hi @CODER_JATIN just a small modification is needed
instead of comparing starting and ending times of consecutive activities
keep a variable to keep track of the last activity you chose. See if starting time[j] is >= ending time of the activity you last chose. If the condition is satisfied, you can update the last activity you chose as j. Update the value of count.
To start the loop, you can choose the first activity, and then the first activity chosen would be 0. (we are recording the index)

but jese mene kia hai, vo bhi sahi hai , ??? what is the problem in this?

ide.codingblocks.com/s/303211 , now ab mene modification kari hai jese aapne bola tha , test cases to pass ho gye lekin,

ye wala test case dekho 1 3 (10 20) (5 7) (21 43) , is test case ki output 2 aani chahiye lekin is code ke hisaab se 3 aa rhi hai…

@CODER_JATIN it will be 3 only?
5-7
10-20
21-43

You can perform all these 3 activities, there is no overlap

Hanji , understood , thanks ji.

1 Like

@CODER_JATIN dont forget to mark your doubt as resolved!