forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_circles_to_be_removed.cpp
71 lines (62 loc) · 1.7 KB
/
min_circles_to_be_removed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Problem Statement:
Given n Circles present in x-y plane such that all the circles have their centre aligned on the x-axis.
The task is to remove some of them, such that no two circles are intersecting.
Find the minimum number of circles that need to be removed. */
#include <bits/stdc++.h>
using namespace std;
/* this function will help in sorting the circles on the
basis of their radius */
bool compare(pair<int, int> a, pair<int, int> b)
{
if ((a.first + a.second) == (b.first + b.second))
{
return (a.first - a.second) < (b.first - b.second);
}
else
{
return (a.first + a.second) < (b.first + b.second);
}
}
int main()
{
int ans = 1;
int n, center, radius;
cout<<"Enter total number of circles: "<<endl;
cin >> n;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++)
{
cout<<"Enter the center and the radius respectively: "<<endl;
cin >> center >> radius;
v.push_back(make_pair(center, radius));
}
sort(v.begin(), v.end(), compare);
int val = v[0].first + v[0].second;
for (int i = 0; i < n - 1; i++)
{
if (val <= v[i + 1].first - v[i + 1].second)
{
ans++;
val = v[i + 1].first + v[i + 1].second;
}
}
cout <<"Minimum circles that are to be removed: "<< (n - ans) <<endl;
return 0;
}
/*Example:
1)Input:
Enter total number of circles:
4
Enter the center and the radius respectively:
1 1
Enter the center and the radius respectively:
2 1
Enter the center and the radius respectively:
3 1
Enter the center and the radius respectively:
4 1
Output:
Minimum circles that are to be removed: 2
Time Complexity: O(nlogn)
Space Complexity: O(nlogn)
*/