AfterAcademy Tech
•
16 Jun 2020

Difficulty: Hard
Asked in: Amazon, Google
Problem Description
Given N point on a 2D plane as pair of (x, y) coordinates, Write a program to find the maximum number of point which lie on the same line.
Example 1
Input: A[][] = [[1,1],[2,2],[1,2],[3,3],[2,3]]
Output: 3
Explanation: The maximum number of point which lie on same line are 3, those points are [1,1], [2, 2] and [3, 3]
Example 2
Input: A[][] = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation: The maximum number of point which lie on same line are 4, those points are [3,2], [4,1], [2,3] and [1, 4].
We will be discussing two different ways to solve this problem
max_pts count.You may try this problem here.
To compute the number of points on a line, we need to
Solution Steps
max_pts that will store the maximum number of points that will be lying on the same line.Pseudo Code
// check whether the three points are on the same line
int coline(int[] a,int[] b,int[] c){
if (a[0]-c[0])*(b[1]-c[1]) == (a[1]-c[1])*(b[0]-c[0])
return 1
else
return 0
}
int maxPoints(int[][] points) {
int n = points.size()
int max_pts = 0
for(int i = 0 to i < n){
for(int j = i+1 to j < n){
if(points[i][0]==points[j][0] && points[i][1]==points[j][1])
continue
int sum = 0
for(int k = 0 to k < n)
sum += coline(points[i],points[j],points[k])
max_pts = max(max_pts,sum)
}
}
if(max_pts==0)
return n
else
return max_pts
}
Complexity Analysis
Time Complexity: O(n³)
Space Complexity: O(1)
Critical Ideas to Think
A line passing through two points has a unique slope. One could deduce that if we take a point p1 from the set of points and draw lines from p1 to all other points, then each of them will give a slope and if there are same slopes for multiple points, then definitely, the points with the same slope will be on the same line or you can say collinear. So, if you will think about it, when we pick points one by one from the set of points and find slopes for every other point, we removed the possibility of parallel lines for each iteration. Thus, maintaining a hash map of slope will help us identify the maximum number of points lying on the same line.
Solution Steps
(x2 — x1) / (y2 — y1) . Use double instead of float to maintain the precision level.overlap variable to check for repeating points and create a vertical variable to count the number of points lying on the line parallel to the Y-axis.res to store the maximum number of points lying on the same line.Pseudo Code
int maxPoints(int[][] points) {
int max_pts = 0
for (int i = 0 to i < points.size()) {
int localmax = 0, overlap = 1, vertical = 1;
map<double, int> m
for (int j = i + 1 to j < points.size()) {
if (points[i][0] == points[j][0] && points[i][1] == points[j][1])
overlap = overlap + 1
else
if (points[i][0] == points[j][0])
vertical = vertical + 1
else {
slope = double(points[i][1] - points[j][1]) / (points[i][0] - points[j][0])
m[slope] = m[slope] + 1
}
}
for (slope in m)
localmax = max(m[slope], localmax)
max_pts = max(vertical, max(localmax + overlap, max_pts))
}
return max_pts
}
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(n)
Critical Ideas to Think
A and tried to find the slope for the lines starting from A to every other point B and stored into a map. A unique line is determined both by the slope and intercept. How did we manage to maintain a counter for each slope without checking for intercept?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding!
Enjoy Algorithms!
AfterAcademy Tech
Given array representation of min Heap, write a program to convert it to max Heap. This problem will clear the concepts of the heap and priority queue which is a very important concept of data structures.

AfterAcademy Tech
You are given an array arr[] with n elements. Write a program to find the contiguous subarray which has the largest product. It's a typical optimization problem.

AfterAcademy Tech
You are given the head of a linked list which probably contains a loop. If the list contains a loop, you need to find the last node of the list which points to one of its previous nodes to create a loop and make it point to NULL, thereby removing the loop.

AfterAcademy Tech
In this blog, we will learn about the various differences between SQL and SQL server based on some points.
