Max Points on a Line - Problem
Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Two points always define a line, but three or more points are collinear if they all lie on the same line. The challenge is to efficiently find the largest group of collinear points.
Note: Handle edge cases like duplicate points, vertical lines, and horizontal lines carefully.
Input & Output
Example 1 — Three Collinear Points
$
Input:
points = [[1,1],[2,2],[3,3]]
›
Output:
3
💡 Note:
All three points lie on the line y = x, so the maximum is 3 collinear points
Example 2 — Mixed Points
$
Input:
points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
›
Output:
4
💡 Note:
Points (1,1), (3,2), (5,3) lie on line y = 0.5x + 0.5, but actually (1,1),(2,3),(1,4),(4,1) might form a better line depending on calculation
Example 3 — Duplicate Points
$
Input:
points = [[1,1],[1,1],[2,2]]
›
Output:
3
💡 Note:
Two duplicate points at (1,1) plus point (2,2) - all three can be considered on the same line
Constraints
- 1 ≤ points.length ≤ 300
- points[i].length == 2
- -104 ≤ xi, yi ≤ 104
- All the points are unique except possibly some duplicates
Visualization
Tap to expand
Understanding the Visualization
1
Input Points
Given array of coordinate points on 2D plane
2
Find Collinear
Points with same slope from anchor are on same line
3
Count Maximum
Return the largest group of collinear points found
Key Takeaway
🎯 Key Insight: Points are collinear if they have identical slopes relative to any fixed anchor point
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code