Party Halls Doubt


Whats wrong with this approach ?

Your approach simply sorts the array depending on the start time only. You just have to find the maximum number of parties going on at a particular time. So instead of pushing the pair {a,b}, push the pairs {a,0} and {b,1}. Then sort the array. This way all your start times will come first. Start traversing in your vector.
When you encounter v[i].second==1, increase your counter, otherwise decrease it. Also keep track of the max value reached. Finally display the max value stored.

I could not undrstand the logic

Your goal is just to find the maximum number of parties at a given time. So what you can do is either pick a time and check for each range if that time falls in the range. If it falls, you increase your counter. And you do this for each time and get the final answer.
I will explain my approach with an example. Let the time ranges be -
(0,2), (1,5), (2,4), (4,6)
Answer here should be 3
Create a pair vector as follows ->
(0,2) -> (0,0), (2,1)
(1,5) -> (1,0), (5,1)
(2,4) -> (2,0), (4,1)
(4,6) -> (4,0), (6,1)

Now sort the 8 values obtained using inbuilt sort function. And initialize a variable as 0. When v[i].second=0, it means a party has started so increment your counter. In the other case, a party has ended, so decrement it.
(0,0) (1,0) (2,0) (2,1) (4,0) (4,1) (5,1) (6,1)
1 2 3 2 3 2 1 0

Maximum value of counter is 3. So we need minimum 3 party halls.