Maximum Circle Greedy

I know the approach, that we need to take the starting and end points of each circle and calculate total number of non overlapping circles and then subtract it with total number of circles. But in my case what i have calculated total number of overlapping circles just, and it isn’t coming out to be correct. Moreover can how can i optimize it, as it is using 2 nested loops.

Here is the code:

hi @Kishan-Mishra-2464674013761441
approach for this question can be ,
Consider, for each circle, its beginning c-r at and ending at c+r . Now greedily select activities by sorting them according to their ending.

1 Like

I used the same approach. I have updated the code. Now i have sorted the ending points of all activities(circles) and solved for all non overlapping activities. Still getting answer as 3, for sample input. Answer should be 2.
Updated Code:

your approach is correct but the values you are trying to input in the vector of pairs is not correct.
and your compare function is also little bit incorrect


i have added comments you can check.

Ma’am i have erased everything i wrote. And just made all integers as long long int. Even your code is giving output 3 for sample input :
4
1 1
2 1
3 1
4 1
Output should be 2.
Code: https://ide.codingblocks.com/s/104452

i will check again my code.

check this code

1 Like

All the test cases are passed. One thing i wanted to confirm, you initialized ans with 1. Is it because we have already taken val = v[0].first + v[0].second, and then ran a loop from 0 to n-1?

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.

can you explain me your comp function please, i don’t understand