Input: points =[(0,0),(1,1),(2,2),(0,1)]Output: Line y = x (passes through(0,0),(1,1),(2,2))Explanation: Three points lie on the diagonal line with slope 1 and intercept 0, which is the maximum.
Input: points =[(2,0),(2,1),(2,2),(0,0),(1,1)]Output: Line x =2(passes through(2,0),(2,1),(2,2))Explanation: A vertical line at x=2 contains three points — more than any other line.
This is the brute force solution. We take point 1, and then point 2 and make a line. Now in the third nested loop, check if point 3 is existing on the same line or not.
Line findLineWithMaxPoint(Set points){
foreach Point p1 in points
foreach Point p2 in (points - {p1}){
Line l = makeLine(p1,p2);
int count = 2 //line contains p1 and p2 foreach(Point p3 in (points-{p1,p2})){
if(l.contains(p3))
count++;
}
if(maxCount<count){
count = maxCount;
resultLine = l;
}
}
return resultLine;
}
Treat each point as a base and count how many other points share the same slope relative to that base. Points that coincide with the base are counted as duplicates. The slope that appears most frequently for a base defines the best line through that base; the global best is the maximum over all bases.
Create a hash map from a normalized slope key to its count and maintain a duplicates counter for points identical to p.
For every other point q, compute the slope from p to q and convert it to a robust key (e.g., reduced dy/dx or a high-precision string); use a special key for vertical lines.
Increment the count for that slope and track the local maximum for this base.
The total points on the best line through p = local_max + duplicates + 1 (including p). Update the global best when larger.
After processing all bases return the line (represented by slope and intercept or by a base point plus slope) with the highest count.
Complexity: O(n^2) time and O(n) extra space per base (for the hash map).
classSolution {
staticfinaldouble EPS = 1e-8;
staticclassPoint { finaldouble x, y; Point(double x, double y) { this.x= x; this.y= y; } }
staticclassLine { finalboolean vertical; finaldouble slope, intercept, x; Line(double slope, double intercept) { this.vertical=false; this.slope= slope; this.intercept= intercept; this.x= Double.NaN; } Line(double x) { this.vertical=true; this.slope= Double.POSITIVE_INFINITY; this.intercept= Double.NaN; this.x= x; } booleancontains(Point p) { return vertical ? Math.abs(p.x- x) < EPS : Math.abs(p.y- (slope * p.x+ intercept)) < EPS; } }
public Line findLineWithMaxPoints(java.util.List<Point> points) {
int n = points.size(); if (n < 2) returnnull;
Line ans =null; int ansCount = 0;
for (int i = 0; i < n; ++i) {
java.util.Map<String, Integer> map =new java.util.HashMap<>(); int duplicates = 0, localMax = 0;
Point a = points.get(i);
for (int j = i + 1; j < n; ++j) {
Point b = points.get(j);
if (Math.abs(a.x- b.x) < EPS && Math.abs(a.y- b.y) < EPS) { duplicates++; continue; }
String key = Math.abs(a.x- b.x) < EPS ?"VERTICAL" : String.format("S:%.12f", (b.y- a.y) / (b.x- a.x));
int cnt = map.getOrDefault(key, 0) + 1; map.put(key, cnt); localMax = Math.max(localMax, cnt);
}
int countWithA = localMax + duplicates + 1;
if (countWithA > ansCount) {
ansCount = countWithA; String bestKey =null;
for (java.util.Map.Entry<String,Integer> e : map.entrySet()) if (e.getValue() == localMax) { bestKey = e.getKey(); break; }
if (bestKey ==null|| bestKey.equals("VERTICAL")) ans =new Line(a.x);
else { double slope = Double.parseDouble(bestKey.substring(2)); ans =new Line(slope, a.y- slope * a.x); }
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dataclassPoint(val x: Double, val y: Double)
classSolution {
funfindLineWithMaxPoints(points: List<Point>): Line {
var ansCount = 0var ans = Line(0.0, 0.0)
for (i in points.indices) {
for (j in i+1 until points.size) {
val line = Line((points[j].y - points[i].y) / (points[j].x - points[i].x), (points[j].x * points[i].y - points[i].x * points[j].y) / (points[j].x - points[i].x))
var cnt = 0for (p in points) if (line.contains(p)) cnt++if (cnt > ansCount) { ansCount = cnt; ans = line }
}
}
return ans
}
}
The Hough Transform is a technique from image processing to detect lines by mapping points in Cartesian space to parameters in Hough space. It can efficiently find the most common line among a set of points.