-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointsontheStraightLine.cpp
More file actions
52 lines (42 loc) · 1.02 KB
/
PointsontheStraightLine.cpp
File metadata and controls
52 lines (42 loc) · 1.02 KB
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
/*Question:
Points on the Straight Line
Asked in:
Google
Amazon
InMobi
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Sample Input :
(1, 1)
(2, 2)
Sample Output :
2
You will be given 2 arrays X and Y. Each point is represented by (X[i], Y[i])
*/
int Solution::maxPoints(vector<int> &A, vector<int> &B) {
int maxi=0;
if(A.size()<=2)
return A.size();
for(int i=0;i<A.size();i++)
{
map<pair<int,int>,int> v;
for(int j=0;j<A.size();j++)
{
if(i!=j){
int y=B[j]-B[i];
int x=A[j]-A[i];
int hc=__gcd(x,y);
if(hc==0)
{
hc=1;
}
v[(make_pair(y/hc,x/hc))]+=1;
}
}
for(auto i=v.begin();i!=v.end();i++)
{
// cout<<(i->first).first<<" "<<(i->first).second<<" :"<<i->second<<endl;
maxi=max(maxi,i->second+1);
}
}
return maxi;
}