Max Points on a Line - Problem

Given an array of points where points[i] = [x_i, y_i] represents coordinates on a 2D plane, find the maximum number of points that lie on the same straight line.

This is a classic computational geometry problem that tests your understanding of slope calculations, hash tables, and mathematical precision. You'll need to consider edge cases like vertical lines, identical points, and floating-point precision issues.

Key Challenge: Two points always form a line, but finding the maximum number of collinear points requires checking all possible line combinations efficiently.

Input & Output

example_1.py โ€” Basic collinear points
$ Input: points = [[1,1],[2,2],[3,3]]
โ€บ Output: 3
๐Ÿ’ก Note: All three points lie on the same line y = x, so the maximum number of collinear points is 3.
example_2.py โ€” Mixed collinear groups
$ Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
โ€บ Output: 4
๐Ÿ’ก Note: Points [1,1], [3,2], [5,3] form one line, and points [1,4], [2,3], [4,1] form another line. Both contain 3 points, but we can find 4 points on a single line.
example_3.py โ€” Edge case with duplicates
$ Input: points = [[1,1],[1,1],[2,2]]
โ€บ Output: 3
๐Ÿ’ก Note: Even with duplicate points at [1,1], all points are collinear on the line y = x, giving us 3 total points.

Visualization

Tap to expand
๐ŸŒŸ Maximum Collinear Points: 4 stars align!Golden Line4 points alignedBlue Line3 points alignedRed Points2 isolated points
Understanding the Visualization
1
Choose Anchor Point
Select a reference point to calculate slopes from
2
Calculate All Slopes
Compute normalized slopes from anchor to every other point
3
Group by Slope
Points with identical slopes are collinear with the anchor
4
Count Maximum Group
Find the largest group plus the anchor point
5
Try All Anchors
Repeat process with each point as anchor to find global maximum
Key Takeaway
๐ŸŽฏ Key Insight: By fixing each point as an anchor and grouping others by slope, we efficiently find all collinear combinations without checking every possible line explicitly. The slope grouping method reduces complexity from O(nยณ) to O(nยฒ) while handling precision issues through rational slope representation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n points, calculate slopes to all other n points. Hash operations are O(1) average case.

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Hash table to store slope frequencies for each anchor point, maximum n-1 entries per iteration

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค points.length โ‰ค 300
  • points[i].length == 2
  • -104 โ‰ค xi, yi โ‰ค 104
  • All points are unique except for duplicates explicitly mentioned
Asked in
Google 45 Apple 38 Meta 32 Microsoft 28 Amazon 25
67.5K Views
Medium Frequency
~35 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen